
Vertex = Struct.new(:id, :adj_list, :distance) do
	def to_s
		"#{id} #{distance || -1}"
	end
end

def bfs!(vertexes, distance)
	next_list = vertexes.select{ |v| v.distance.nil? }.inject([]) do |result, v| 
		v.distance = distance
		v.adj_list.select{ |v| v.distance.nil? }.inject(result) { |r, adj| r << adj }
	end

	bfs!(next_list, distance + 1) unless next_list.empty?
end

n = STDIN.gets.to_i
vertex_list = Array.new(n) { |i| Vertex.new(i + 1) }
n.times do
	vertex_no, degree, *adj_list = STDIN.gets.split.map(&:to_i)

	vertex = vertex_list[vertex_no - 1]
	vertex.adj_list = adj_list[0...degree].map! { |adj| vertex_list[adj - 1] }
end

bfs!([vertex_list[0]], 0)
vertex_list.each { |v| puts v }