def bfs(u)
	$q.push([u, 0])
	$d[u] = 0
	while $q != []
		f = $q.first
		distance = f[1] + 1
		$l[f[0]].each do |i|
			if $d[i] == nil
				$q.push([i, f[1]+1]) if i != u
				$d[i] = distance
			end
		end
		$q.shift
	end
end



n = gets.chomp.to_i
$l=[]
$q = []
$d = Array.new(n+1)
n.times do
	u, k, *v = gets.chomp.split(" ").map(&:to_i)
	$l[u] = v
end
bfs(1)
$d.each_with_index do |val,i|
	next if i == 0
	val = -1 if val == nil
	puts "#{i} #{val}"
end
