class Graph
  def initialize(id, next_id)
    @id = id
    @next_id = next_id
    @d = -1
  end

  attr_accessor :id, :next_id, :d
end


def breadth_first_search(first)
  first.d = 0
  queue = [first]

  until queue.empty? do
    now = queue.shift
    now.next_id.each do |x|
      if @array[x].d == -1
        @array[x].d = now.d+1
        queue << @array[x]
      end
    end
  end
end


@array = [nil]

n = gets.to_i.times do
  input = gets.split(" ").map{|x| x.to_i}
  u = input.shift
  k = input.shift
  @array << Graph.new(u, input)
end

breadth_first_search(@array[1])

1.upto n do |x|
  printf "%d %d\n" ,@array[x].id ,@array[x].d
end

