N = int(input())
E = [[] for _ in range(N+1)]
for _ in range(N):
	tmp = list(map(int, input().split()))
	if tmp[1] == 0:
		continue
	E[tmp[0]] = tmp[2:]

cnt = [0 for _ in range(N+1)]
q = [1]

while q:
	cp = q.pop(0)
	for np in E[cp]:
		if cnt[np] != 0:
			continue
		
		cnt[np] = cnt[cp] + 1
		q.append(np)

for ind, cost in enumerate(cnt):
	if ind == 0:
		continue
	if ind == 1:
		print(ind, 0)
	else:
		if cost == 0:
			print(ind, -1)
		else:
			print(ind, cost) 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) [] E = [[] for _ in range(N+1)] E [[] for _ in range(N+1)] for _ in range(N):
	tmp = list(map(int, input().split()))
	if tmp[1] == 0:
		continue
	E[tmp[0]] = tmp[2:] _ range(N) range N tmp = list(map(int, input().split())) tmp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split if tmp[1] == 0:
		continue
	 tmp[1] == 0 tmp[1] tmp 1 0 continue E[tmp[0]] = tmp[2:] E[tmp[0]] E tmp[0] tmp 0 tmp[2:] tmp 2: 2 0 for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) 0 cnt = [0 for _ in range(N+1)] cnt [0 for _ in range(N+1)] q = [1] q [1] 1 while q:
	cp = q.pop(0)
	for np in E[cp]:
		if cnt[np] != 0:
			continue
		
		cnt[np] = cnt[cp] + 1
		q.append(np) q cp = q.pop(0) cp q.pop(0) q.pop q pop 0 for np in E[cp]:
		if cnt[np] != 0:
			continue
		
		cnt[np] = cnt[cp] + 1
		q.append(np) np E[cp] E cp if cnt[np] != 0:
			continue
		
		 cnt[np] != 0 cnt[np] cnt np 0 continue cnt[np] = cnt[cp] + 1 cnt[np] cnt np cnt[cp] + 1 cnt[cp] cnt cp 1 q.append(np) q.append q append np for ind, cost in enumerate(cnt):
	if ind == 0:
		continue
	if ind == 1:
		print(ind, 0)
	else:
		if cost == 0:
			print(ind, -1)
		else:
			print(ind, cost) ind cost enumerate(cnt) enumerate cnt if ind == 0:
		continue
	 ind == 0 ind 0 continue if ind == 1:
		print(ind, 0)
	else:
		if cost == 0:
			print(ind, -1)
		else:
			print(ind, cost) ind == 1 ind 1 print(ind, 0) print ind 0 if cost == 0:
			print(ind, -1)
		else:
			print(ind, cost) cost == 0 cost 0 print(ind, -1) print ind -1 print(ind, cost) print ind cost cnt = [0 for _ in range(N+1)] [0 for _ in range(N+1)] cnt cp = q.pop(0) q.pop(0) cp q = [1] [1] q tmp = list(map(int, input().split())) list(map(int, input().split())) tmp N = int(input()) int(input()) N E = [[] for _ in range(N+1)] [[] for _ in range(N+1)] E
N = int(input())
E = [[] for _ in range(N+1)]
for _ in range(N):
	tmp = list(map(int, input().split()))
	if tmp[1] == 0:
		continue
	E[tmp[0]] = tmp[2:]

cnt = [0 for _ in range(N+1)]
q = [1]

while q:
	cp = q.pop(0)
	for np in E[cp]:
		if cnt[np] != 0:
			continue
		
		cnt[np] = cnt[cp] + 1
		q.append(np)

for ind, cost in enumerate(cnt):
	if ind == 0:
		continue
	if ind == 1:
		print(ind, 0)
	else:
		if cost == 0:
			print(ind, -1)
		else:
			print(ind, cost)
