INFTY = (1<<21)



def bfs(s)
	q = [s]
	d = Array.new($n, INFTY)
	d[s] = 0

	while !q.empty?
		u = q.shift
		$n.times do |v|
			next if $m[u][v] == 0
			next if d[v] != INFTY
			d[v] = d[u] + 1
			q.push(v)
		end
	end

	$n.times do |i|
		print i+1, " ", (d[i] == INFTY ? -1 : d[i]), "\n"
	end
end



$n = gets.to_i
$m = Array.new($n){ Array.new($n, 0) }

$n.times do
	u, k, *adj_list = gets.split.map &:to_i
	u -= 1
	adj_list.each do |v|
		v -= 1
		$m[u][v] = 1
	end
end

bfs(0)