n = int(input())
G = {}
D = [-1] * n
for _ in range(n):
    A = list(map(int, input().split()))
    G[A[0] - 1] = [a - 1 for a in A[2:]]

# print(G)

q = [(0, 0)]
while len(q) > 0:
    v, d = q[0]
    del q[0]
    if D[v] >= 0:
        continue
    D[v] = d

    for nv in G[v]:
        q.append((nv, d+1))

for i in range(n):
    print(i+1, D[i])
 n = int(input()) n int(input()) int input() input G = {} G {} D = [-1] * n D [-1] * n [-1] -1 n for _ in range(n):
    A = list(map(int, input().split()))
    G[A[0] - 1] = [a - 1 for a in A[2:]]

# print(G) _ range(n) range n A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split a - 1 for a in A[2:] for a in A[2:] a A[2:] A 2: 2 for a in A[2:] a - 1 a 1 G[A[0] - 1] = [a - 1 for a in A[2:]] G[A[0] - 1] G A[0] - 1 A[0] A 0 1 [a - 1 for a in A[2:]] q = [(0, 0)] q [(0, 0)] (0, 0) 0 0 while len(q) > 0:
    v, d = q[0]
    del q[0]
    if D[v] >= 0:
        continue
    D[v] = d

    for nv in G[v]:
        q.append((nv, d+1)) len(q) > 0 len(q) len q 0 v, d = q[0] v q[0] q 0 d del q[0] q[0] q 0 if D[v] >= 0:
        continue
     D[v] >= 0 D[v] D v 0 continue D[v] = d D[v] D v d for nv in G[v]:
        q.append((nv, d+1)) nv G[v] G v q.append((nv, d+1)) q.append q append (nv, d+1) nv d+1 d 1 for i in range(n):
    print(i+1, D[i]) i range(n) range n print(i+1, D[i]) print i+1 i 1 D[i] D i G = {} {} G D = [-1] * n [-1] * n D q = [(0, 0)] [(0, 0)] q d = q[0] q[0] d n = int(input()) int(input()) n v, d = q[0] q[0] v A = list(map(int, input().split())) list(map(int, input().split())) A
n = int(input())
G = {}
D = [-1] * n
for _ in range(n):
    A = list(map(int, input().split()))
    G[A[0] - 1] = [a - 1 for a in A[2:]]

# print(G)

q = [(0, 0)]
while len(q) > 0:
    v, d = q[0]
    del q[0]
    if D[v] >= 0:
        continue
    D[v] = d

    for nv in G[v]:
        q.append((nv, d+1))

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

