from collections import deque
N = int(input())
#グラフ入力受取
G = [[] for _ in range(N)]
for _ in range(N):
    a = list(map(int, input().split()))
    for i in a[2:]:
        G[a[0]-1].append(i-1)

#頂点1をスタートとした探索
dist = [-1]*N
que = deque()
dist[0] = 0
que.append(0)
while len(que)!=0:
    v = que.popleft()
    for nv in G[v]:
        if dist[nv] == -1:
            dist[nv] = dist[v]+1
            que.append(nv)
        
for i in range(N):
    print(str(i+1) + ' ' + str(dist[i])) from collections import deque N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] G = [[] for _ in range(N)] G [[] for _ in range(N)] for _ in range(N):
    a = list(map(int, input().split()))
    for i in a[2:]:
        G[a[0]-1].append(i-1)

#頂点1をスタートとした探索 _ 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 for i in a[2:]:
        G[a[0]-1].append(i-1)

#頂点1をスタートとした探索 i a[2:] a 2: 2 G[a[0]-1].append(i-1) [a[0]-1].append [a[0]-1] G a[0]-1 a[0] a 0 1 append i-1 i 1 dist = [-1]*N dist [-1]*N [-1] -1 N que = deque() que deque() deque dist[0] = 0 dist[0] dist 0 0 que.append(0) que.append que append 0 while len(que)!=0:
    v = que.popleft()
    for nv in G[v]:
        if dist[nv] == -1:
            dist[nv] = dist[v]+1
            que.append(nv)
         len(que)!=0 len(que) len que 0 v = que.popleft() v que.popleft() que.popleft que popleft for nv in G[v]:
        if dist[nv] == -1:
            dist[nv] = dist[v]+1
            que.append(nv)
         nv G[v] G v if dist[nv] == -1:
            dist[nv] = dist[v]+1
            que.append(nv)
         dist[nv] == -1 dist[nv] dist nv -1 dist[nv] = dist[v]+1 dist[nv] dist nv dist[v]+1 dist[v] dist v 1 que.append(nv) que.append que append nv for i in range(N):
    print(str(i+1) + ' ' + str(dist[i])) i range(N) range N print(str(i+1) + ' ' + str(dist[i])) print str(i+1) + ' ' + str(dist[i]) str(i+1) + ' ' str(i+1) str i+1 i 1 ' ' str(dist[i]) str dist[i] dist i G = [[] for _ in range(N)] [[] for _ in range(N)] G dist = [-1]*N [-1]*N dist N = int(input()) int(input()) N a = list(map(int, input().split())) list(map(int, input().split())) a que = deque() deque() que v = que.popleft() que.popleft() v
from collections import deque
N = int(input())
#グラフ入力受取
G = [[] for _ in range(N)]
for _ in range(N):
    a = list(map(int, input().split()))
    for i in a[2:]:
        G[a[0]-1].append(i-1)

#頂点1をスタートとした探索
dist = [-1]*N
que = deque()
dist[0] = 0
que.append(0)
while len(que)!=0:
    v = que.popleft()
    for nv in G[v]:
        if dist[nv] == -1:
            dist[nv] = dist[v]+1
            que.append(nv)
        
for i in range(N):
    print(str(i+1) + ' ' + str(dist[i]))
