class Node
  attr_accessor :id, :children, :visited, :d, :f
  
  def initialize(id)
    @id = id
    @children = []
    @visited = false
    @d = nil
    @f = nil
  end 

  def add_child(node)
    @children << node
  end

  def visit
    @visited = true
  end

  def to_s
    "#{@id}"
  end

  def unvisited_child
    self.children.each do | child |
      return child unless child.visited
    end
    nil
  end

  def dfs_recursive
    self.visit
    self.children.each do | child |
      child.dfs unless child.visited
    end
  end

  def dfs_stack(time)
    self.visit
    stack = [self]
    self.d = time
    while not stack.empty?
      time += 1
      u = stack.last
      v = u.unvisited_child
      if v
        v.visit
        v.d = time
        stack << v
      else
        v = stack.pop
        v.f = time
      end
    end
    return time
  end

end

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

n.times do
  # v -> '親', '子の数', ['子', ...]
  v = gets.split.map(&:to_i)
  id = v.shift; v.shift
  v.each do | cid |
    tree[id].add_child(tree[cid])
  end
end

time = 0
1.upto(n) do | i |
  time = tree[i].dfs_stack(time+1) unless tree[i].d
end

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

