from typing import List
from collections import deque

def bfs(G: List[int], s: int) -> None:
	d[s] = 0
	color[s] = "GRAY"
	Q = deque()
	Q.append(s)
	while Q:
		u = Q.popleft()
		for v in G[u]:
			if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		color[u] = "BLACK"


if __name__ == "__main__":
	n = int(input())
	G = [[] for _ in range(n)]
	for _ in range(n):
		u, k, *adj = list(map(int, input().split()))
		u -= 1
		G[u] = [v - 1 for v in adj]
	color = ["WHITE" for _ in range(n)]
	d = [-1 for _ in range(n)]
	pi = [None for _ in range(n)]
	bfs(G, 0)
	for i in range(n):
		print(f"{i + 1} {d[i]}") from typing import List from collections import deque def bfs(G: List[int], s: int) -> None:
	d[s] = 0
	color[s] = "GRAY"
	Q = deque()
	Q.append(s)
	while Q:
		u = Q.popleft()
		for v in G[u]:
			if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		color[u] = "BLACK" bfs d[s] = 0 d[s] d s 0 color[s] = "GRAY" color[s] color s "GRAY" Q = deque() Q deque() deque Q.append(s) Q.append Q append s while Q:
		u = Q.popleft()
		for v in G[u]:
			if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		color[u] = "BLACK" Q u = Q.popleft() u Q.popleft() Q.popleft Q popleft for v in G[u]:
			if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		 v G[u] G u if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		 color[v] == "WHITE" color[v] color v "WHITE" color[v] = "GRAY" color[v] color v "GRAY" d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 pi[v] = u pi[v] pi v u Q.append(v) Q.append Q append v color[u] = "BLACK" color[u] color u "BLACK" G: List[int] G s: int s if __name__ == "__main__":
	n = int(input())
	G = [[] for _ in range(n)]
	for _ in range(n):
		u, k, *adj = list(map(int, input().split()))
		u -= 1
		G[u] = [v - 1 for v in adj]
	color = ["WHITE" for _ in range(n)]
	d = [-1 for _ in range(n)]
	pi = [None for _ in range(n)]
	bfs(G, 0)
	for i in range(n):
		print(f"{i + 1} {d[i]}") __name__ == "__main__" __name__ "__main__" n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] G = [[] for _ in range(n)] G [[] for _ in range(n)] for _ in range(n):
		u, k, *adj = list(map(int, input().split()))
		u -= 1
		G[u] = [v - 1 for v in adj]
	 _ range(n) range n u, k, *adj = list(map(int, input().split())) u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split k *adj adj u -= 1 u 1 v - 1 for v in adj for v in adj v adj for v in adj v - 1 v 1 G[u] = [v - 1 for v in adj] G[u] G u [v - 1 for v in adj] "WHITE" for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) "WHITE" color = ["WHITE" for _ in range(n)] color ["WHITE" for _ in range(n)] -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 d = [-1 for _ in range(n)] d [-1 for _ in range(n)] None for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) None pi = [None for _ in range(n)] pi [None for _ in range(n)] bfs(G, 0) bfs G 0 for i in range(n):
		print(f"{i + 1} {d[i]}") i range(n) range n print(f"{i + 1} {d[i]}") print f"{i + 1} {d[i]}" n = int(input()) int(input()) n pi = [None for _ in range(n)] [None for _ in range(n)] pi color = ["WHITE" for _ in range(n)] ["WHITE" for _ in range(n)] color d = [-1 for _ in range(n)] [-1 for _ in range(n)] d u -= 1 1 u k, *adj = list(map(int, input().split())) list(map(int, input().split())) k u, k, *adj = list(map(int, input().split())) list(map(int, input().split())) u def bfs(G: List[int], s: int) -> None:
	d[s] = 0
	color[s] = "GRAY"
	Q = deque()
	Q.append(s)
	while Q:
		u = Q.popleft()
		for v in G[u]:
			if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		color[u] = "BLACK" def bfs(G: List[int], s: int) -> None:
	d[s] = 0
	color[s] = "GRAY"
	Q = deque()
	Q.append(s)
	while Q:
		u = Q.popleft()
		for v in G[u]:
			if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		color[u] = "BLACK" bfs G = [[] for _ in range(n)] [[] for _ in range(n)] G
from typing import List
from collections import deque

def bfs(G: List[int], s: int) -> None:
	d[s] = 0
	color[s] = "GRAY"
	Q = deque()
	Q.append(s)
	while Q:
		u = Q.popleft()
		for v in G[u]:
			if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		color[u] = "BLACK"


if __name__ == "__main__":
	n = int(input())
	G = [[] for _ in range(n)]
	for _ in range(n):
		u, k, *adj = list(map(int, input().split()))
		u -= 1
		G[u] = [v - 1 for v in adj]
	color = ["WHITE" for _ in range(n)]
	d = [-1 for _ in range(n)]
	pi = [None for _ in range(n)]
	bfs(G, 0)
	for i in range(n):
		print(f"{i + 1} {d[i]}")
