class DFSdata
  def initialize(id, next_id)
    @id = id
    @next_id = next_id
    @first = 0
    @finish = 0
  end

  attr_accessor  :id, :next_id, :first, :finish
end

def depth_first_search
  @array[1].first = 1
  stack = [@array[1]]
  count = 2

  ct = 0

  until stack.empty? do
    now = stack.pop
    child = now.next_id.map{|x|x}
    child.each do |x|
      if @array[x].first == 0
        @array[x].first = count
        count += 1
        stack << now
        stack << @array[x]
        break
      else
        now.next_id.delete(x)
      end
    end

    if now.next_id.empty?
      now.finish = count
      count += 1
    end

  end

end


@array = [nil]

n = gets.to_i.times do
  input = gets.split(" ").map{|x| x.to_i}
  u = input.shift
  k = input.shift
  @array << DFSdata.new(u, input)
end

depth_first_search()

1.upto n do |i|
  printf "%d %d %d\n" ,@array[i].id ,@array[i].first ,@array[i].finish
end

