def breadth_first_search ms, visit_qs, paths
  i = visit_qs.shift
  ms[i].each do |a|
    if paths[a - 1] == -1
      paths[a - 1] = paths[i] + 1
      visit_qs << a - 1
    end
  end
  breadth_first_search ms, visit_qs, paths if visit_qs.length > 0
end

n = gets.to_i
ms = Array.new(n)
paths = Array.new(n, -1)
n.times do
  num, _, *adjacents = gets.chomp.split(' ').map(&:to_i)
  ms[num - 1] = adjacents
end
paths[0] = 0
breadth_first_search ms, [0], paths
paths.each_with_index do |p, i|
  puts "#{i + 1} #{p}"
end
