n = int(input())
G = {i:[] for i in range(1, n+1)}
for _ in range(n):
    ukv = list(map(int, input().split()))
    u = ukv[0]
    k = ukv[1]
    for i in range(2, 2+k):
        G[u].append(ukv[i])
        
stack = [1]
dist = [-1]*(n+1)
dist[1] = 0

while stack:
    target = stack.pop(0)
    for target_i in G[target]:
        if dist[target_i]==-1:
            dist[target_i] = dist[target]+1
            stack.append(target_i)
            
for i, dist_i in enumerate(dist[1:], 1):
    print(i, dist_i) n = int(input()) n int(input()) int input() input i:[] for i in range(1, n+1) for i in range(1, n+1) i range(1, n+1) range 1 n+1 n 1 for i in range(1, n+1) i [] G = {i:[] for i in range(1, n+1)} G {i:[] for i in range(1, n+1)} for _ in range(n):
    ukv = list(map(int, input().split()))
    u = ukv[0]
    k = ukv[1]
    for i in range(2, 2+k):
        G[u].append(ukv[i])
         _ range(n) range n ukv = list(map(int, input().split())) ukv list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u = ukv[0] u ukv[0] ukv 0 k = ukv[1] k ukv[1] ukv 1 for i in range(2, 2+k):
        G[u].append(ukv[i])
         i range(2, 2+k) range 2 2+k 2 k G[u].append(ukv[i]) [u].append [u] G u append ukv[i] ukv i stack = [1] stack [1] 1 dist = [-1]*(n+1) dist [-1]*(n+1) [-1] -1 n+1 n 1 dist[1] = 0 dist[1] dist 1 0 while stack:
    target = stack.pop(0)
    for target_i in G[target]:
        if dist[target_i]==-1:
            dist[target_i] = dist[target]+1
            stack.append(target_i)
             stack target = stack.pop(0) target stack.pop(0) stack.pop stack pop 0 for target_i in G[target]:
        if dist[target_i]==-1:
            dist[target_i] = dist[target]+1
            stack.append(target_i)
             target_i G[target] G target if dist[target_i]==-1:
            dist[target_i] = dist[target]+1
            stack.append(target_i)
             dist[target_i]==-1 dist[target_i] dist target_i -1 dist[target_i] = dist[target]+1 dist[target_i] dist target_i dist[target]+1 dist[target] dist target 1 stack.append(target_i) stack.append stack append target_i for i, dist_i in enumerate(dist[1:], 1):
    print(i, dist_i) i dist_i enumerate(dist[1:], 1) enumerate dist[1:] dist 1: 1 1 print(i, dist_i) print i dist_i stack = [1] [1] stack u = ukv[0] ukv[0] u k = ukv[1] ukv[1] k target = stack.pop(0) stack.pop(0) target G = {i:[] for i in range(1, n+1)} {i:[] for i in range(1, n+1)} G ukv = list(map(int, input().split())) list(map(int, input().split())) ukv dist = [-1]*(n+1) [-1]*(n+1) dist n = int(input()) int(input()) n
n = int(input())
G = {i:[] for i in range(1, n+1)}
for _ in range(n):
    ukv = list(map(int, input().split()))
    u = ukv[0]
    k = ukv[1]
    for i in range(2, 2+k):
        G[u].append(ukv[i])
        
stack = [1]
dist = [-1]*(n+1)
dist[1] = 0

while stack:
    target = stack.pop(0)
    for target_i in G[target]:
        if dist[target_i]==-1:
            dist[target_i] = dist[target]+1
            stack.append(target_i)
            
for i, dist_i in enumerate(dist[1:], 1):
    print(i, dist_i)
