n=int(input())
a=[]
from collections import deque as dq
for i in range(n):
	aaa=list(map(int,input().split()))
	aa=aaa[2:]
	a.append(aa)
dis=[-1]*n
dis[0]=0
queue=dq([0])
while queue:
	p=queue.popleft()
	for i in range(len(a[p])):
		if dis[a[p][i]-1]==-1:
			queue.append(a[p][i]-1)
			dis[a[p][i]-1]=dis[p]+1
for i in range(n):
	print(i+1,dis[i])
		
 n=int(input()) n int(input()) int input() input a=[] a [] from collections import deque as dq for i in range(n):
	aaa=list(map(int,input().split()))
	aa=aaa[2:]
	a.append(aa) i range(n) range n aaa=list(map(int,input().split())) aaa list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split aa=aaa[2:] aa aaa[2:] aaa 2: 2 a.append(aa) a.append a append aa dis=[-1]*n dis [-1]*n [-1] -1 n dis[0]=0 dis[0] dis 0 0 queue=dq([0]) queue dq([0]) dq [0] 0 while queue:
	p=queue.popleft()
	for i in range(len(a[p])):
		if dis[a[p][i]-1]==-1:
			queue.append(a[p][i]-1)
			dis[a[p][i]-1]=dis[p]+1 queue p=queue.popleft() p queue.popleft() queue.popleft queue popleft for i in range(len(a[p])):
		if dis[a[p][i]-1]==-1:
			queue.append(a[p][i]-1)
			dis[a[p][i]-1]=dis[p]+1 i range(len(a[p])) range len(a[p]) len a[p] a p if dis[a[p][i]-1]==-1:
			queue.append(a[p][i]-1)
			dis[a[p][i]-1]=dis[p]+1 dis[a[p][i]-1]==-1 dis[a[p][i]-1] dis a[p][i]-1 a[p][i] [p] a p i 1 -1 queue.append(a[p][i]-1) queue.append queue append a[p][i]-1 a[p][i] [p] a p i 1 dis[a[p][i]-1]=dis[p]+1 dis[a[p][i]-1] dis a[p][i]-1 a[p][i] [p] a p i 1 dis[p]+1 dis[p] dis p 1 for i in range(n):
	print(i+1,dis[i])
		 i range(n) range n print(i+1,dis[i]) print i+1 i 1 dis[i] dis i aaa=list(map(int,input().split())) list(map(int,input().split())) aaa queue=dq([0]) dq([0]) queue p=queue.popleft() queue.popleft() p aa=aaa[2:] aaa[2:] aa dis=[-1]*n [-1]*n dis n=int(input()) int(input()) n a=[] [] a
n=int(input())
a=[]
from collections import deque as dq
for i in range(n):
	aaa=list(map(int,input().split()))
	aa=aaa[2:]
	a.append(aa)
dis=[-1]*n
dis[0]=0
queue=dq([0])
while queue:
	p=queue.popleft()
	for i in range(len(a[p])):
		if dis[a[p][i]-1]==-1:
			queue.append(a[p][i]-1)
			dis[a[p][i]-1]=dis[p]+1
for i in range(n):
	print(i+1,dis[i])
		

