import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

from collections import deque
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
g = []
for i in range(n):
    t = na()
    u = t[0] - 1
    k = t[1]
    v = [x-1 for x in t[2:]]
    g.append(v)
dist = [-1] * n
que = deque()
dist[0] = 0
que.append(0)
while que:
    #print(que)
    v = que.popleft()
    for nv in g[v]:
        if dist[nv] != -1: continue
        dist[nv] = dist[v] + 1
        que.append(nv)

for i in range(n):
    print(i+1, dist[i])
 import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time math string itertools fractions heapq collections re array bisect sys random time from collections import deque sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 inf = 10**20 inf 10**20 10 20 mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 stdin = sys.stdin stdin sys.stdin sys stdin ni = lambda: int(ns()) ni lambda: int(ns()) int(ns()) int ns() ns na = lambda: list(map(int, stdin.readline().split())) na lambda: list(map(int, stdin.readline().split())) list(map(int, stdin.readline().split())) list map(int, stdin.readline().split()) map int stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split ns = lambda: stdin.readline().rstrip() ns lambda: stdin.readline().rstrip() stdin.readline().rstrip() stdin.readline().rstrip stdin.readline() stdin.readline stdin readline rstrip n = ni() n ni() ni g = [] g [] for i in range(n):
    t = na()
    u = t[0] - 1
    k = t[1]
    v = [x-1 for x in t[2:]]
    g.append(v) i range(n) range n t = na() t na() na u = t[0] - 1 u t[0] - 1 t[0] t 0 1 k = t[1] k t[1] t 1 x-1 for x in t[2:] for x in t[2:] x t[2:] t 2: 2 for x in t[2:] x-1 x 1 v = [x-1 for x in t[2:]] v [x-1 for x in t[2:]] g.append(v) g.append g append v dist = [-1] * n dist [-1] * n [-1] -1 n que = deque() que deque() deque dist[0] = 0 dist[0] dist 0 0 que.append(0) que.append que append 0 while que:
    #print(que)
    v = que.popleft()
    for nv in g[v]:
        if dist[nv] != -1: continue
        dist[nv] = dist[v] + 1
        que.append(nv) que v = que.popleft() v que.popleft() que.popleft que popleft for nv in g[v]:
        if dist[nv] != -1: continue
        dist[nv] = dist[v] + 1
        que.append(nv) nv g[v] g v if dist[nv] != -1: continue
         dist[nv] != -1 dist[nv] dist nv -1 dist[nv] = dist[v] + 1 dist[nv] dist nv dist[v] + 1 dist[v] dist v 1 que.append(nv) que.append que append nv for i in range(n):
    print(i+1, dist[i]) i range(n) range n print(i+1, dist[i]) print i+1 i 1 dist[i] dist i t = na() na() t dist = [-1] * n [-1] * n dist n = ni() ni() n ni = lambda: int(ns()) lambda: int(ns()) ni stdin = sys.stdin sys.stdin stdin u = t[0] - 1 t[0] - 1 u g = [] [] g v = que.popleft() que.popleft() v ns = lambda: stdin.readline().rstrip() lambda: stdin.readline().rstrip() ns inf = 10**20 10**20 inf mod = 10**9 + 7 10**9 + 7 mod k = t[1] t[1] k v = [x-1 for x in t[2:]] [x-1 for x in t[2:]] v na = lambda: list(map(int, stdin.readline().split())) lambda: list(map(int, stdin.readline().split())) na que = deque() deque() que
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

from collections import deque
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
g = []
for i in range(n):
    t = na()
    u = t[0] - 1
    k = t[1]
    v = [x-1 for x in t[2:]]
    g.append(v)
dist = [-1] * n
que = deque()
dist[0] = 0
que.append(0)
while que:
    #print(que)
    v = que.popleft()
    for nv in g[v]:
        if dist[nv] != -1: continue
        dist[nv] = dist[v] + 1
        que.append(nv)

for i in range(n):
    print(i+1, dist[i])

