n = STDIN.gets.to_i
matrix    = Array.new(n) { Array.new(n, false) }
distances = Array.new(n)

n.times do
	vertex, degree, *adj_list = STDIN.gets.split.map(&:to_i)
	degree.times do |i|
		matrix[vertex - 1][adj_list[i] - 1] = true
	end
end

distances[0] = 0
distance = 1
queue = [].push(0)
until queue.empty?
	v = queue.shift
	adj_list = matrix[v].each_index.
		select { |i| matrix[v][i] && distances[i].nil? }.
		each { |adj| distances[adj] = distance }

	next if adj_list.empty?
	queue.push(*adj_list)
	distance += 1
end

matrix.each_index { |i| puts "#{i + 1} #{distances[i] || -1}" }