class Node
  attr_reader :id
  attr_accessor :parent
  protected :parent=

  def initialize(id)
    @id = id
    @children = []
  end

  def children
    @children.to_enum
  end

  def add_child(child)
    @children << child
    child.parent = self
  end

  def type
    if @parent.nil?
      'root'
    elsif @children.empty?
      'leaf'
    else
      'internal node'
    end
  end

  def depth
    if frozen?
      @depth
    else
      _depth
    end
  end

  private def _depth
    @parent.nil? ? 0 : @parent.depth + 1
  end

  def parent_id
    @parent.nil? ? -1 : @parent.id
  end

  alias_method :_freeze, :freeze
  private :_freeze

  # Freeze from its root to its all leaves.
  def freeze
    return @parent.freeze if @parent
    freeze_self_and_children
  end

  protected def freeze_self_and_children
    @depth = _depth
    _freeze
    @children.freeze
    @children.each { |child| child.freeze_self_and_children }
  end
end

class NodeRepository
  include Enumerable

  def initialize
    @store = {}
  end

  def each(&block)
    Enumerator.new do |y|
      @store.sort.each { |kv| y << kv }
    end.each(&block)
  end

  def [](id)
    @store[id] ||= Node.new(id)
  end
end
repo = NodeRepository.new

n = gets.chomp.to_i
n.times do
  id, _, *child_ids = gets.chomp.split(' ').map(&:to_i)
  parent = repo[id] # To create a node when children is none.
  child_ids.each { |c_id| parent.add_child(repo[c_id]) }
end

repo.first[1].freeze

repo.each do |_, node|
  puts "node #{node.id}: parent = #{node.parent_id}, " +
       "depth = #{node.depth}, #{node.type}, " +
       "[#{node.children.map(&:id).join(', ')}]"
end

