class Node
  attr_reader :val, :child
  attr_accessor :d, :f
  def initialize(i, child)
    @val = i
    @child = child
    @d = -1
  end
end

class Graph
  attr_accessor :node
  def initialize(n)
    @node = Array.new(n)
  end
  def breadth_first_search
    @node[0].d = 0
    q = [0]
    ii = 1
    loop do
      break if q.size == 0
      next_q = []
      q.each{|i|
        @node[i].child.each{|ch|
          if @node[ch-1].d < 0 then
            @node[ch-1].d = ii
            next_q << ch-1
          end
        }
      }
      q = next_q
      ii += 1
    end
  end
  def print
    @node.size.times{|i|
      puts "#{@node[i].val} #{@node[i].d}"
    }
  end
end

n = gets.to_i
graph = Graph.new(n)
n.times{
  i, *child = gets.split.map(&:to_i)
  child.shift
  graph.node[i-1] = Node.new(i, child)
}

graph.breadth_first_search
graph.print