def breadth_first_search(G):
  n = len(G)
  d = [-1] * (n + 1)
  d[1] = 0
  queue = [1]

  while len(queue) > 0:
    v = queue.pop(0)
    for c in G[v]:
      if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

  for i in range(1, n + 1):
    print(i, d[i])

G = {}
for i in range(int(input())):
  x = list(map(int, input().split()))
  G[x[0]] = x[2:]

breadth_first_search(G) def breadth_first_search(G):
  n = len(G)
  d = [-1] * (n + 1)
  d[1] = 0
  queue = [1]

  while len(queue) > 0:
    v = queue.pop(0)
    for c in G[v]:
      if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

  for i in range(1, n + 1):
    print(i, d[i]) breadth_first_search n = len(G) n len(G) len G d = [-1] * (n + 1) d [-1] * (n + 1) [-1] -1 n + 1 n 1 d[1] = 0 d[1] d 1 0 queue = [1] queue [1] 1 while len(queue) > 0:
    v = queue.pop(0)
    for c in G[v]:
      if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

   len(queue) > 0 len(queue) len queue 0 v = queue.pop(0) v queue.pop(0) queue.pop queue pop 0 for c in G[v]:
      if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

   c G[v] G v if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

   d[c] < 0 d[c] d c 0 d[c] = d[v] + 1 d[c] d c d[v] + 1 d[v] d v 1 queue.append(c) queue.append queue append c for i in range(1, n + 1):
    print(i, d[i]) i range(1, n + 1) range 1 n + 1 n 1 print(i, d[i]) print i d[i] d i G G G = {} G {} for i in range(int(input())):
  x = list(map(int, input().split()))
  G[x[0]] = x[2:] i range(int(input())) range int(input()) int input() input x = list(map(int, input().split())) x list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split G[x[0]] = x[2:] G[x[0]] G x[0] x 0 x[2:] x 2: 2 breadth_first_search(G) breadth_first_search G x = list(map(int, input().split())) list(map(int, input().split())) x def breadth_first_search(G):
  n = len(G)
  d = [-1] * (n + 1)
  d[1] = 0
  queue = [1]

  while len(queue) > 0:
    v = queue.pop(0)
    for c in G[v]:
      if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

  for i in range(1, n + 1):
    print(i, d[i]) def breadth_first_search(G):
  n = len(G)
  d = [-1] * (n + 1)
  d[1] = 0
  queue = [1]

  while len(queue) > 0:
    v = queue.pop(0)
    for c in G[v]:
      if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

  for i in range(1, n + 1):
    print(i, d[i]) breadth_first_search G = {} {} G
def breadth_first_search(G):
  n = len(G)
  d = [-1] * (n + 1)
  d[1] = 0
  queue = [1]

  while len(queue) > 0:
    v = queue.pop(0)
    for c in G[v]:
      if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

  for i in range(1, n + 1):
    print(i, d[i])

G = {}
for i in range(int(input())):
  x = list(map(int, input().split()))
  G[x[0]] = x[2:]

breadth_first_search(G)