from collections import deque
n = int(input())
data = [None]*n
for _ in range(n):
    a,*b = map(int,input().split())
    a -= 1
    data[a] = [i-1 for i in b[1:]]
d = [-1]*n
dq = deque([(0,0)])
while dq:
    p,q = dq.popleft()
    if d[p] == -1:
        d[p] = q
        for i in data[p]:
            dq.append((i,q+1))
for i,j in enumerate(d):
    print(i+1,j) from collections import deque n = int(input()) n int(input()) int input() input data = [None]*n data [None]*n [None] None n for _ in range(n):
    a,*b = map(int,input().split())
    a -= 1
    data[a] = [i-1 for i in b[1:]] _ range(n) range n a,*b = map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split *b b a -= 1 a 1 i-1 for i in b[1:] for i in b[1:] i b[1:] b 1: 1 for i in b[1:] i-1 i 1 data[a] = [i-1 for i in b[1:]] data[a] data a [i-1 for i in b[1:]] d = [-1]*n d [-1]*n [-1] -1 n dq = deque([(0,0)]) dq deque([(0,0)]) deque [(0,0)] (0,0) 0 0 while dq:
    p,q = dq.popleft()
    if d[p] == -1:
        d[p] = q
        for i in data[p]:
            dq.append((i,q+1)) dq p,q = dq.popleft() p dq.popleft() dq.popleft dq popleft q if d[p] == -1:
        d[p] = q
        for i in data[p]:
            dq.append((i,q+1)) d[p] == -1 d[p] d p -1 d[p] = q d[p] d p q for i in data[p]:
            dq.append((i,q+1)) i data[p] data p dq.append((i,q+1)) dq.append dq append (i,q+1) i q+1 q 1 for i,j in enumerate(d):
    print(i+1,j) i j enumerate(d) enumerate d print(i+1,j) print i+1 i 1 j dq = deque([(0,0)]) deque([(0,0)]) dq n = int(input()) int(input()) n p,q = dq.popleft() dq.popleft() p data = [None]*n [None]*n data d = [-1]*n [-1]*n d a -= 1 1 a q = dq.popleft() dq.popleft() q a,*b = map(int,input().split()) map(int,input().split()) a
from collections import deque
n = int(input())
data = [None]*n
for _ in range(n):
    a,*b = map(int,input().split())
    a -= 1
    data[a] = [i-1 for i in b[1:]]
d = [-1]*n
dq = deque([(0,0)])
while dq:
    p,q = dq.popleft()
    if d[p] == -1:
        d[p] = q
        for i in data[p]:
            dq.append((i,q+1))
for i,j in enumerate(d):
    print(i+1,j)
