from collections import deque
n=int(input())
edge=[[] for _ in range(n+1)]
for i in range(n):
  v,k,*u=map(int,input().split())
  edge[v]=u
p=[-1]*(n+1)
p[1]=0
q=deque([])
q.append(1)
v=[1]*(n+1)
v[1]=0
while q:
  now=q.popleft()
  for i in edge[now]:
    if v[i]:
      q.append(i)
      p[i]=p[now]+1
      v[i]=0
for i in range(1,n+1):
  print(i,p[i]) from collections import deque n=int(input()) n int(input()) int input() input [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] edge=[[] for _ in range(n+1)] edge [[] for _ in range(n+1)] for i in range(n):
  v,k,*u=map(int,input().split())
  edge[v]=u i range(n) range n v,k,*u=map(int,input().split()) v map(int,input().split()) map int input().split() ().split () input split k *u u edge[v]=u edge[v] edge v u p=[-1]*(n+1) p [-1]*(n+1) [-1] -1 n+1 n 1 p[1]=0 p[1] p 1 0 q=deque([]) q deque([]) deque [] q.append(1) q.append q append 1 v=[1]*(n+1) v [1]*(n+1) [1] 1 n+1 n 1 v[1]=0 v[1] v 1 0 while q:
  now=q.popleft()
  for i in edge[now]:
    if v[i]:
      q.append(i)
      p[i]=p[now]+1
      v[i]=0 q now=q.popleft() now q.popleft() q.popleft q popleft for i in edge[now]:
    if v[i]:
      q.append(i)
      p[i]=p[now]+1
      v[i]=0 i edge[now] edge now if v[i]:
      q.append(i)
      p[i]=p[now]+1
      v[i]=0 v[i] v i q.append(i) q.append q append i p[i]=p[now]+1 p[i] p i p[now]+1 p[now] p now 1 v[i]=0 v[i] v i 0 for i in range(1,n+1):
  print(i,p[i]) i range(1,n+1) range 1 n+1 n 1 print(i,p[i]) print i p[i] p i v,k,*u=map(int,input().split()) map(int,input().split()) v now=q.popleft() q.popleft() now edge=[[] for _ in range(n+1)] [[] for _ in range(n+1)] edge v=[1]*(n+1) [1]*(n+1) v n=int(input()) int(input()) n p=[-1]*(n+1) [-1]*(n+1) p q=deque([]) deque([]) q k,*u=map(int,input().split()) map(int,input().split()) k
from collections import deque
n=int(input())
edge=[[] for _ in range(n+1)]
for i in range(n):
  v,k,*u=map(int,input().split())
  edge[v]=u
p=[-1]*(n+1)
p[1]=0
q=deque([])
q.append(1)
v=[1]*(n+1)
v[1]=0
while q:
  now=q.popleft()
  for i in edge[now]:
    if v[i]:
      q.append(i)
      p[i]=p[now]+1
      v[i]=0
for i in range(1,n+1):
  print(i,p[i])
