class Node
  attr_accessor :id, :children, :visited, :depth

  def initialize(id)
    @id = id
    @children = []
    @visited = false
    @depth = -1
  end

  def add_child(child)
    @children << child
  end

  def visit
    self.visited = true
  end

  def to_s
    "#{@id}"
  end

  def bfs
    self.depth = 0
    que = [self]
    self.visit
    while not que.empty?
      n = que.shift
      n.children.each do | c |
        c.depth = n.depth + 1 if c.depth == -1
        que << c unless c.visited
        c.visit
      end
    end
  end

end

n = gets.to_i
tree = Array.new(n+1){ | i | Node.new(i) }
tree[0] = nil

n.times do
  ukv = gets.split.map(&:to_i)
  u = ukv.shift; ukv.shift
  ukv.each do | v |
    tree[u].add_child(tree[v])
  end
end

tree[1].bfs

1.upto(n) do | i |
  puts "#{tree[i].id} #{tree[i].depth}"
end

