n = int(input())
a = []
for i in range(n):
  tmp = list(map(int, input().split()))
  a.append(tmp[2:])
ans = [-1 for _ in range(n + 1)]
ans[1] = 0
que = [1]
while len(que) > 0:
  current = que.pop(0)
  if len(a[current - 1]) == 0:
    continue
  for next in a[current - 1]:
    if ans[next] == -1:
      que.append(next)
      ans[next] = ans[current] + 1
for i in range(1, n + 1):
  print(i, ans[i])
     n = int(input()) n int(input()) int input() input a = [] a [] for i in range(n):
  tmp = list(map(int, input().split()))
  a.append(tmp[2:]) i range(n) range n tmp = list(map(int, input().split())) tmp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split a.append(tmp[2:]) a.append a append tmp[2:] tmp 2: 2 -1 for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) -1 ans = [-1 for _ in range(n + 1)] ans [-1 for _ in range(n + 1)] ans[1] = 0 ans[1] ans 1 0 que = [1] que [1] 1 while len(que) > 0:
  current = que.pop(0)
  if len(a[current - 1]) == 0:
    continue
  for next in a[current - 1]:
    if ans[next] == -1:
      que.append(next)
      ans[next] = ans[current] + 1 len(que) > 0 len(que) len que 0 current = que.pop(0) current que.pop(0) que.pop que pop 0 if len(a[current - 1]) == 0:
    continue
   len(a[current - 1]) == 0 len(a[current - 1]) len a[current - 1] a current - 1 current 1 0 continue for next in a[current - 1]:
    if ans[next] == -1:
      que.append(next)
      ans[next] = ans[current] + 1 next a[current - 1] a current - 1 current 1 if ans[next] == -1:
      que.append(next)
      ans[next] = ans[current] + 1 ans[next] == -1 ans[next] ans next -1 que.append(next) que.append que append next ans[next] = ans[current] + 1 ans[next] ans next ans[current] + 1 ans[current] ans current 1 for i in range(1, n + 1):
  print(i, ans[i])
     i range(1, n + 1) range 1 n + 1 n 1 print(i, ans[i]) print i ans[i] ans i a = [] [] a current = que.pop(0) que.pop(0) current tmp = list(map(int, input().split())) list(map(int, input().split())) tmp que = [1] [1] que n = int(input()) int(input()) n ans = [-1 for _ in range(n + 1)] [-1 for _ in range(n + 1)] ans
n = int(input())
a = []
for i in range(n):
  tmp = list(map(int, input().split()))
  a.append(tmp[2:])
ans = [-1 for _ in range(n + 1)]
ans[1] = 0
que = [1]
while len(que) > 0:
  current = que.pop(0)
  if len(a[current - 1]) == 0:
    continue
  for next in a[current - 1]:
    if ans[next] == -1:
      que.append(next)
      ans[next] = ans[current] + 1
for i in range(1, n + 1):
  print(i, ans[i])
    
