from collections import deque

def bfs():
	que=deque([])
	que.append(0)
	v[0]=0
	
	while que:
		p=que.popleft()
		for l in L[p]:
			if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	
n=int(input())
L=[]
for i in range(n):
	A=[]
	A=list(map(int,input().split()))
	L.append(A[2:])
v=[-1]*n
bfs()
for i in range(n):
	print(i+1,v[i])
 from collections import deque def bfs():
	que=deque([])
	que.append(0)
	v[0]=0
	
	while que:
		p=que.popleft()
		for l in L[p]:
			if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	 bfs que=deque([]) que deque([]) deque [] que.append(0) que.append que append 0 v[0]=0 v[0] v 0 0 while que:
		p=que.popleft()
		for l in L[p]:
			if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	 que p=que.popleft() p que.popleft() que.popleft que popleft for l in L[p]:
			if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	 l L[p] L p if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	 v[l-1]==-1 v[l-1] v l-1 l 1 -1 que.append(l-1) que.append que append l-1 l 1 v[l-1]=v[p]+1 v[l-1] v l-1 l 1 v[p]+1 v[p] v p 1 n=int(input()) n int(input()) int input() input L=[] L [] for i in range(n):
	A=[]
	A=list(map(int,input().split()))
	L.append(A[2:]) i range(n) range n A=[] A [] A=list(map(int,input().split())) A list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split L.append(A[2:]) L.append L append A[2:] A 2: 2 v=[-1]*n v [-1]*n [-1] -1 n bfs() bfs for i in range(n):
	print(i+1,v[i]) i range(n) range n print(i+1,v[i]) print i+1 i 1 v[i] v i A=list(map(int,input().split())) list(map(int,input().split())) A A=[] [] A def bfs():
	que=deque([])
	que.append(0)
	v[0]=0
	
	while que:
		p=que.popleft()
		for l in L[p]:
			if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	 def bfs():
	que=deque([])
	que.append(0)
	v[0]=0
	
	while que:
		p=que.popleft()
		for l in L[p]:
			if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	 bfs v=[-1]*n [-1]*n v L=[] [] L n=int(input()) int(input()) n
from collections import deque

def bfs():
	que=deque([])
	que.append(0)
	v[0]=0
	
	while que:
		p=que.popleft()
		for l in L[p]:
			if v[l-1]==-1:
				que.append(l-1)
				v[l-1]=v[p]+1
	
n=int(input())
L=[]
for i in range(n):
	A=[]
	A=list(map(int,input().split()))
	L.append(A[2:])
v=[-1]*n
bfs()
for i in range(n):
	print(i+1,v[i])

