import collections
n = int(input())
G = []
for i in range(n):
    T = [0]*n
    G.append(T)
for i in range(n):
    L = list(map(int,input().split()))
    for j in range(2,len(L)):
        G[i][L[j]-1] += 1
d = [float('inf')]*n
q = collections.deque()
q.append(0)
d[0] = 0
while len(q) > 0:
    start = q.popleft()
    for i in range(n):
        if G[start][i] == 1:
            if d[i] == float('inf'):
                q.append(i)
                d[i] = d[start]+1
for i in range(n):
    if d[i] != float('inf'):
        print(i+1,d[i])
    else:
        print(i+1,-1) import collections collections n = int(input()) n int(input()) int input() input G = [] G [] for i in range(n):
    T = [0]*n
    G.append(T) i range(n) range n T = [0]*n T [0]*n [0] 0 n G.append(T) G.append G append T for i in range(n):
    L = list(map(int,input().split()))
    for j in range(2,len(L)):
        G[i][L[j]-1] += 1 i range(n) range n L = list(map(int,input().split())) L list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for j in range(2,len(L)):
        G[i][L[j]-1] += 1 j range(2,len(L)) range 2 len(L) len L G[i][L[j]-1] += 1 G[i][L[j]-1] [i] G i L[j]-1 L[j] L j 1 1 d = [float('inf')]*n d [float('inf')]*n [float('inf')] float('inf') float 'inf' n q = collections.deque() q collections.deque() collections.deque collections deque q.append(0) q.append q append 0 d[0] = 0 d[0] d 0 0 while len(q) > 0:
    start = q.popleft()
    for i in range(n):
        if G[start][i] == 1:
            if d[i] == float('inf'):
                q.append(i)
                d[i] = d[start]+1 len(q) > 0 len(q) len q 0 start = q.popleft() start q.popleft() q.popleft q popleft for i in range(n):
        if G[start][i] == 1:
            if d[i] == float('inf'):
                q.append(i)
                d[i] = d[start]+1 i range(n) range n if G[start][i] == 1:
            if d[i] == float('inf'):
                q.append(i)
                d[i] = d[start]+1 G[start][i] == 1 G[start][i] [start] G start i 1 if d[i] == float('inf'):
                q.append(i)
                d[i] = d[start]+1 d[i] == float('inf') d[i] d i float('inf') float 'inf' q.append(i) q.append q append i d[i] = d[start]+1 d[i] d i d[start]+1 d[start] d start 1 for i in range(n):
    if d[i] != float('inf'):
        print(i+1,d[i])
    else:
        print(i+1,-1) i range(n) range n if d[i] != float('inf'):
        print(i+1,d[i])
    else:
        print(i+1,-1) d[i] != float('inf') d[i] d i float('inf') float 'inf' print(i+1,d[i]) print i+1 i 1 d[i] d i print(i+1,-1) print i+1 i 1 -1 T = [0]*n [0]*n T d = [float('inf')]*n [float('inf')]*n d n = int(input()) int(input()) n q = collections.deque() collections.deque() q L = list(map(int,input().split())) list(map(int,input().split())) L start = q.popleft() q.popleft() start G = [] [] G
import collections
n = int(input())
G = []
for i in range(n):
    T = [0]*n
    G.append(T)
for i in range(n):
    L = list(map(int,input().split()))
    for j in range(2,len(L)):
        G[i][L[j]-1] += 1
d = [float('inf')]*n
q = collections.deque()
q.append(0)
d[0] = 0
while len(q) > 0:
    start = q.popleft()
    for i in range(n):
        if G[start][i] == 1:
            if d[i] == float('inf'):
                q.append(i)
                d[i] = d[start]+1
for i in range(n):
    if d[i] != float('inf'):
        print(i+1,d[i])
    else:
        print(i+1,-1)
