#coding: UTF-8
from collections import deque
n = int(input())
link = [[] for i in range(n+1)]
for i in range(n):
  r = list(map(int,input().split()))
  u = r.pop(0)
  k = r.pop(0)
  link[u] = r

searched=set()
queue = deque()
d = [-1 for i in range(n+1)]

v = 1
d[v] = 0
queue.append(v)
searched.add(v)
while queue:
  v = queue.popleft()
  for w in link[v]:
    if w not in searched:
      d[w] = d[v] + 1
      queue.append(w)
      searched.add(w)

for i in range(1,n+1):
  print(str(i) + " " + str(d[i]))
 from collections import deque n = int(input()) n int(input()) int input() input [] for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) [] link = [[] for i in range(n+1)] link [[] for i in range(n+1)] for i in range(n):
  r = list(map(int,input().split()))
  u = r.pop(0)
  k = r.pop(0)
  link[u] = r i range(n) range n r = list(map(int,input().split())) r list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split u = r.pop(0) u r.pop(0) r.pop r pop 0 k = r.pop(0) k r.pop(0) r.pop r pop 0 link[u] = r link[u] link u r searched=set() searched set() set queue = deque() queue deque() deque -1 for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) -1 d = [-1 for i in range(n+1)] d [-1 for i in range(n+1)] v = 1 v 1 d[v] = 0 d[v] d v 0 queue.append(v) queue.append queue append v searched.add(v) searched.add searched add v while queue:
  v = queue.popleft()
  for w in link[v]:
    if w not in searched:
      d[w] = d[v] + 1
      queue.append(w)
      searched.add(w) queue v = queue.popleft() v queue.popleft() queue.popleft queue popleft for w in link[v]:
    if w not in searched:
      d[w] = d[v] + 1
      queue.append(w)
      searched.add(w) w link[v] link v if w not in searched:
      d[w] = d[v] + 1
      queue.append(w)
      searched.add(w) w not in searched w searched d[w] = d[v] + 1 d[w] d w d[v] + 1 d[v] d v 1 queue.append(w) queue.append queue append w searched.add(w) searched.add searched add w for i in range(1,n+1):
  print(str(i) + " " + str(d[i])) i range(1,n+1) range 1 n+1 n 1 print(str(i) + " " + str(d[i])) print str(i) + " " + str(d[i]) str(i) + " " str(i) str i " " str(d[i]) str d[i] d i r = list(map(int,input().split())) list(map(int,input().split())) r d = [-1 for i in range(n+1)] [-1 for i in range(n+1)] d queue = deque() deque() queue k = r.pop(0) r.pop(0) k v = queue.popleft() queue.popleft() v searched=set() set() searched u = r.pop(0) r.pop(0) u v = 1 1 v link = [[] for i in range(n+1)] [[] for i in range(n+1)] link n = int(input()) int(input()) n
#coding: UTF-8
from collections import deque
n = int(input())
link = [[] for i in range(n+1)]
for i in range(n):
  r = list(map(int,input().split()))
  u = r.pop(0)
  k = r.pop(0)
  link[u] = r

searched=set()
queue = deque()
d = [-1 for i in range(n+1)]

v = 1
d[v] = 0
queue.append(v)
searched.add(v)
while queue:
  v = queue.popleft()
  for w in link[v]:
    if w not in searched:
      d[w] = d[v] + 1
      queue.append(w)
      searched.add(w)

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

