class Node
  @@time = 1
  @@nodes = []
  def initialize(id)
    @id = id
    @discover = false
    @discover_time = nil
    @end_time = nil
    @edge = nil
    @@nodes[id - 1] = self
  end

  def <<(edges)
    @edge = edges
  end

  def search
    if !@discover
      @discover = true
      @discover_time = @@time
      @@time += 1
      @edge.each{|item|
        @@nodes[item - 1].search
      }
      @end_time = @@time
      @@time += 1
    end
  end

  def to_s
    return [@id, @discover_time, @end_time].join(" ")
  end

  def discover?
    return @discover
  end
  
end

def main
  n = gets.to_i
  arr = Array.new(n){|i|Node.new(i + 1)}
  n.times do |i|
    arg =  gets.split(" ").map(&:to_i)
    arr[i] << arg[arg.length - arg[1]..-1]
  end
  arr.each do |item|
    item.search if !item.discover?
    puts item
  end
end
main