class Vertex
  @@index = 0
  def initialize(adjacent)
    @index = @@index += 1
    @adjacent = adjacent
    @distance = -1
  end

  def to_s
    "#{index} #{distance}"
  end

  attr_reader :index
  attr_accessor :adjacent, :distance
end

def breadth_first_search(vertex)
  vertex.distance = 0
  queue = [vertex]
  until queue.empty?
    current = queue.shift
      current.adjacent.each do |x|
        if x.distance == -1
          x.distance = current.distance + 1
          queue << current.adjacent
          queue.flatten!
        end
      end
  end
end

n = gets.to_i
array = Array.new(n).map { Vertex.new([]) }
n.times do
  adjacent = []
  input = gets.split(' ').map(&:to_i)
  unless input[1].zero?
    input[2..-1].each do |index|
      adjacent << array.find { |vertex| vertex.index == index }
    end
    array[input[0] - 1].adjacent = adjacent
  end
end

breadth_first_search array[0]
puts array.map(&:to_s)
