from collections import deque

n = int(input())
vdata = [list(map(int,input().split())) for _ in range(n)]
d = [-1]*n
q = deque()
q.append(vdata[0])
d[0] = 0

while len(q) > 0:
  node = q.popleft()
  c = d[node[0]-1]
  a = node[1]
  for i in range(a):
    b = node[i+2]-1
    if d[b] == -1:
      d[b] = c+1
      q.append(vdata[b])
      
for i in range(n):
  print(i+1, d[i]) from collections import deque n = int(input()) n int(input()) int input() input list(map(int,input().split())) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split vdata = [list(map(int,input().split())) for _ in range(n)] vdata [list(map(int,input().split())) for _ in range(n)] d = [-1]*n d [-1]*n [-1] -1 n q = deque() q deque() deque q.append(vdata[0]) q.append q append vdata[0] vdata 0 d[0] = 0 d[0] d 0 0 while len(q) > 0:
  node = q.popleft()
  c = d[node[0]-1]
  a = node[1]
  for i in range(a):
    b = node[i+2]-1
    if d[b] == -1:
      d[b] = c+1
      q.append(vdata[b])
       len(q) > 0 len(q) len q 0 node = q.popleft() node q.popleft() q.popleft q popleft c = d[node[0]-1] c d[node[0]-1] d node[0]-1 node[0] node 0 1 a = node[1] a node[1] node 1 for i in range(a):
    b = node[i+2]-1
    if d[b] == -1:
      d[b] = c+1
      q.append(vdata[b])
       i range(a) range a b = node[i+2]-1 b node[i+2]-1 node[i+2] node i+2 i 2 1 if d[b] == -1:
      d[b] = c+1
      q.append(vdata[b])
       d[b] == -1 d[b] d b -1 d[b] = c+1 d[b] d b c+1 c 1 q.append(vdata[b]) q.append q append vdata[b] vdata b 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 node = q.popleft() q.popleft() node c = d[node[0]-1] d[node[0]-1] c a = node[1] node[1] a b = node[i+2]-1 node[i+2]-1 b d = [-1]*n [-1]*n d q = deque() deque() q n = int(input()) int(input()) n vdata = [list(map(int,input().split())) for _ in range(n)] [list(map(int,input().split())) for _ in range(n)] vdata
from collections import deque

n = int(input())
vdata = [list(map(int,input().split())) for _ in range(n)]
d = [-1]*n
q = deque()
q.append(vdata[0])
d[0] = 0

while len(q) > 0:
  node = q.popleft()
  c = d[node[0]-1]
  a = node[1]
  for i in range(a):
    b = node[i+2]-1
    if d[b] == -1:
      d[b] = c+1
      q.append(vdata[b])
      
for i in range(n):
  print(i+1, d[i])
