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