class Vertex
  def initialize(id, others)
    @id = id
    @others = others.sort
    @d = -1
    @f = -1
  end

  def visit(time)
    p self
    @d = time
    @others.each do |v1|
      vertex = $vertices.find{|v2| v2.id == v1}
      if vertex.d < 0 then
        time += 1
        time = vertex.visit(time)
      end
    end
    time += 1
    @f = time
    return time
  end

  attr_accessor :id, :others, :d, :f
end

def dfs
  time = 1
  loop do
    index = $vertices.index{|v| v.d < 0}
    break if index == nil
    time = $vertices[index].visit(time)
    time += 1
  end
end

n = gets.chomp.to_i
$vertices = []
(n).times do
  inputs = gets.chomp.split.map(&:to_i)
  others = []
  inputs[1].times do |i|
    others << inputs[2 + i]
  end
  $vertices << Vertex.new(inputs[0], others)
end

$vertices.sort_by!{|v| v.id}
dfs
$vertices.each do |v|
  puts "#{v.id} #{v.d} #{v.f}"
end