from collections import deque
n = int(input())
Ps = []
for _ in range(n):
  lis = list(map(int, input().split()))
  if len(lis)>2:
    Ps.append(lis[2:])
  else:
    Ps.append([])
ans = [-1]*n
ans[0] = 0
queue = deque()
checked = deque()
[queue.append([p, 1]) for p in Ps[0]]
checked.append(1)
while queue:
  t, pa = queue.popleft()
  if not t in checked:
    for i in Ps[t-1]:
      queue.append([i, t])
    ans[t-1] = ans[pa-1]+1
    checked.append(t)
for i, a in enumerate(ans):
  print(i+1, a) from collections import deque n = int(input()) n int(input()) int input() input Ps = [] Ps [] for _ in range(n):
  lis = list(map(int, input().split()))
  if len(lis)>2:
    Ps.append(lis[2:])
  else:
    Ps.append([]) _ range(n) range n lis = list(map(int, input().split())) lis list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split if len(lis)>2:
    Ps.append(lis[2:])
  else:
    Ps.append([]) len(lis)>2 len(lis) len lis 2 Ps.append(lis[2:]) Ps.append Ps append lis[2:] lis 2: 2 Ps.append([]) Ps.append Ps append [] ans = [-1]*n ans [-1]*n [-1] -1 n ans[0] = 0 ans[0] ans 0 0 queue = deque() queue deque() deque checked = deque() checked deque() deque queue.append([p, 1]) for p in Ps[0] for p in Ps[0] p Ps[0] Ps 0 for p in Ps[0] queue.append([p, 1]) queue.append queue append [p, 1] p 1 [queue.append([p, 1]) for p in Ps[0]] checked.append(1) checked.append checked append 1 while queue:
  t, pa = queue.popleft()
  if not t in checked:
    for i in Ps[t-1]:
      queue.append([i, t])
    ans[t-1] = ans[pa-1]+1
    checked.append(t) queue t, pa = queue.popleft() t queue.popleft() queue.popleft queue popleft pa if not t in checked:
    for i in Ps[t-1]:
      queue.append([i, t])
    ans[t-1] = ans[pa-1]+1
    checked.append(t) not t in checked t in checked t checked for i in Ps[t-1]:
      queue.append([i, t])
     i Ps[t-1] Ps t-1 t 1 queue.append([i, t]) queue.append queue append [i, t] i t ans[t-1] = ans[pa-1]+1 ans[t-1] ans t-1 t 1 ans[pa-1]+1 ans[pa-1] ans pa-1 pa 1 1 checked.append(t) checked.append checked append t for i, a in enumerate(ans):
  print(i+1, a) i a enumerate(ans) enumerate ans print(i+1, a) print i+1 i 1 a lis = list(map(int, input().split())) list(map(int, input().split())) lis queue = deque() deque() queue pa = queue.popleft() queue.popleft() pa n = int(input()) int(input()) n t, pa = queue.popleft() queue.popleft() t checked = deque() deque() checked Ps = [] [] Ps ans = [-1]*n [-1]*n ans
from collections import deque
n = int(input())
Ps = []
for _ in range(n):
  lis = list(map(int, input().split()))
  if len(lis)>2:
    Ps.append(lis[2:])
  else:
    Ps.append([])
ans = [-1]*n
ans[0] = 0
queue = deque()
checked = deque()
[queue.append([p, 1]) for p in Ps[0]]
checked.append(1)
while queue:
  t, pa = queue.popleft()
  if not t in checked:
    for i in Ps[t-1]:
      queue.append([i, t])
    ans[t-1] = ans[pa-1]+1
    checked.append(t)
for i, a in enumerate(ans):
  print(i+1, a)
