import queue
def s():
 N=int(input())
 M=[input().split()[2:]for _ in[0]*N]
 q=queue.Queue();q.put(0)
 d=[-1]*N;d[0]=0
 while not q.empty():
  u=q.get()
  for v in M[u]:
   v=int(v)-1
   if d[v]<0:d[v]=d[u]+1;q.put(v)
 for i in range(N):print(i+1,d[i])
if'__main__'==__name__:s() import queue queue def s():
 N=int(input())
 M=[input().split()[2:]for _ in[0]*N]
 q=queue.Queue();q.put(0)
 d=[-1]*N;d[0]=0
 while not q.empty():
  u=q.get()
  for v in M[u]:
   v=int(v)-1
   if d[v]<0:d[v]=d[u]+1;q.put(v)
 for i in range(N):print(i+1,d[i]) s N=int(input()) N int(input()) int input() input input().split()[2:]for _ in[0]*N for _ in[0]*N _ [0]*N [0] 0 N for _ in[0]*N input().split()[2:] ().split() ().split () input split 2: 2 M=[input().split()[2:]for _ in[0]*N] M [input().split()[2:]for _ in[0]*N] q=queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(0) q.put q put 0 d=[-1]*N d [-1]*N [-1] -1 N d[0]=0 d[0] d 0 0 while not q.empty():
  u=q.get()
  for v in M[u]:
   v=int(v)-1
   if d[v]<0:d[v]=d[u]+1;q.put(v)
  not q.empty() q.empty() q.empty q empty u=q.get() u q.get() q.get q get for v in M[u]:
   v=int(v)-1
   if d[v]<0:d[v]=d[u]+1;q.put(v)
  v M[u] M u v=int(v)-1 v int(v)-1 int(v) int v 1 if d[v]<0:d[v]=d[u]+1;q.put(v)
  d[v]<0 d[v] d v 0 for i in range(N):print(i+1,d[i] i range(N) range N if'__main__'==__name__:s() '__main__'==__name__ '__main__' __name__ def s():
 N=int(input())
 M=[input().split()[2:]for _ in[0]*N]
 q=queue.Queue();q.put(0)
 d=[-1]*N;d[0]=0
 while not q.empty():
  u=q.get()
  for v in M[u]:
   v=int(v)-1
   if d[v]<0:d[v]=d[u]+1;q.put(v)
 for i in range(N):print(i+1,d[i]) def s():
 N=int(input())
 M=[input().split()[2:]for _ in[0]*N]
 q=queue.Queue();q.put(0)
 d=[-1]*N;d[0]=0
 while not q.empty():
  u=q.get()
  for v in M[u]:
   v=int(v)-1
   if d[v]<0:d[v]=d[u]+1;q.put(v)
 for i in range(N):print(i+1,d[i]) s
import queue
def s():
 N=int(input())
 M=[input().split()[2:]for _ in[0]*N]
 q=queue.Queue();q.put(0)
 d=[-1]*N;d[0]=0
 while not q.empty():
  u=q.get()
  for v in M[u]:
   v=int(v)-1
   if d[v]<0:d[v]=d[u]+1;q.put(v)
 for i in range(N):print(i+1,d[i])
if'__main__'==__name__:s()
