import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
G = [list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N)]

D = [-1]*N
q = deque([[0, 0]])
while q:
    cur, dis = q.popleft()
    if D[cur] != -1: continue
    D[cur] = dis
    for nex in G[cur]:
        q.append([nex,dis+1])
for i in range(N):
    print(i+1, D[i])
 import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline from collections import deque N = int(input()) N int(input()) int input() input list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) list(map(lambda x: int(x)-1, input().split()))[2:] (map(lambda x: int(x)-1, input().split())) list map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split 2: 2 G = [list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N)] G [list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N)] D = [-1]*N D [-1]*N [-1] -1 N q = deque([[0, 0]]) q deque([[0, 0]]) deque [[0, 0]] [0, 0] 0 0 while q:
    cur, dis = q.popleft()
    if D[cur] != -1: continue
    D[cur] = dis
    for nex in G[cur]:
        q.append([nex,dis+1]) q cur, dis = q.popleft() cur q.popleft() q.popleft q popleft dis if D[cur] != -1: continue
     D[cur] != -1 D[cur] D cur -1 D[cur] = dis D[cur] D cur dis for nex in G[cur]:
        q.append([nex,dis+1]) nex G[cur] G cur q.append([nex,dis+1]) q.append q append [nex,dis+1] nex dis+1 dis 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 N = int(input()) int(input()) N G = [list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N)] [list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N)] G q = deque([[0, 0]]) deque([[0, 0]]) q cur, dis = q.popleft() q.popleft() cur input = sys.stdin.readline sys.stdin.readline input dis = q.popleft() q.popleft() dis D = [-1]*N [-1]*N D
import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
G = [list(map(lambda x: int(x)-1, input().split()))[2:] for _ in range(N)]

D = [-1]*N
q = deque([[0, 0]])
while q:
    cur, dis = q.popleft()
    if D[cur] != -1: continue
    D[cur] = dis
    for nex in G[cur]:
        q.append([nex,dis+1])
for i in range(N):
    print(i+1, D[i])

