from collections import deque
N = int(input())
edge = [[] for _ in range(N+1)]
for i in range(N):
  A = list(map(int, input().split()))
  edge[A[0]] = A[2:]

ans = [-1]*N
ans[0] = 0
d = deque([[1,0]])
while len(d)>0:
  v,dist = d.popleft()
  for w in edge[v]:
    if ans[w-1]==-1:
      ans[w-1]=dist+1
      d.append([w,dist+1])
for i in range(N):
  print(i+1,ans[i]) from collections import deque N = int(input()) N int(input()) int input() input [] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [] edge = [[] for _ in range(N+1)] edge [[] for _ in range(N+1)] for i in range(N):
  A = list(map(int, input().split()))
  edge[A[0]] = A[2:] i 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 edge[A[0]] = A[2:] edge[A[0]] edge A[0] A 0 A[2:] A 2: 2 ans = [-1]*N ans [-1]*N [-1] -1 N ans[0] = 0 ans[0] ans 0 0 d = deque([[1,0]]) d deque([[1,0]]) deque [[1,0]] [1,0] 1 0 while len(d)>0:
  v,dist = d.popleft()
  for w in edge[v]:
    if ans[w-1]==-1:
      ans[w-1]=dist+1
      d.append([w,dist+1]) len(d)>0 len(d) len d 0 v,dist = d.popleft() v d.popleft() d.popleft d popleft dist for w in edge[v]:
    if ans[w-1]==-1:
      ans[w-1]=dist+1
      d.append([w,dist+1]) w edge[v] edge v if ans[w-1]==-1:
      ans[w-1]=dist+1
      d.append([w,dist+1]) ans[w-1]==-1 ans[w-1] ans w-1 w 1 -1 ans[w-1]=dist+1 ans[w-1] ans w-1 w 1 dist+1 dist 1 d.append([w,dist+1]) d.append d append [w,dist+1] w dist+1 dist 1 for i in range(N):
  print(i+1,ans[i]) i range(N) range N print(i+1,ans[i]) print i+1 i 1 ans[i] ans i N = int(input()) int(input()) N v,dist = d.popleft() d.popleft() v d = deque([[1,0]]) deque([[1,0]]) d dist = d.popleft() d.popleft() dist edge = [[] for _ in range(N+1)] [[] for _ in range(N+1)] edge ans = [-1]*N [-1]*N ans A = list(map(int, input().split())) list(map(int, input().split())) A
from collections import deque
N = int(input())
edge = [[] for _ in range(N+1)]
for i in range(N):
  A = list(map(int, input().split()))
  edge[A[0]] = A[2:]

ans = [-1]*N
ans[0] = 0
d = deque([[1,0]])
while len(d)>0:
  v,dist = d.popleft()
  for w in edge[v]:
    if ans[w-1]==-1:
      ans[w-1]=dist+1
      d.append([w,dist+1])
for i in range(N):
  print(i+1,ans[i])
