class Node
  attr_reader :id, :siblings
  attr_accessor :state, :distance

  def initialize(id)
    @id = id
    @siblings = []
    @state = :white
    @distance = nil
  end

  def <<(sibling)
    @siblings << sibling
  end
end

if $PROGRAM_NAME == __FILE__
  n = gets.to_i

  temp = Hash.new {|hash, id| hash[id] = Node.new(id) }

  n.times do
    id, _, *sibling_ids = gets.chomp.split.map(&:to_i)

    node = temp[id]

    sibling_ids.each do |sibling_id|
      node << temp[sibling_id]
    end
  end

  nodes = temp.values.sort_by(&:id)

  first_node = nodes.first
  first_node.distance = 0

  queue = [first_node]

  until queue.empty?
    current = queue.shift

    current.siblings.each do |sibling|
      next unless sibling.state == :white

      sibling.state = :gray
      sibling.distance = current.distance + 1

      queue << sibling
    end

    current.state = :black
  end

  nodes.each do |node|
    puts "#{node.id} #{node.distance || -1}"
  end
end