class Node
  attr_reader :id, :siblings
  attr_accessor :state, :started_at, :finished_at

  def initialize(id)
    @id = id
    @siblings = []
    @state = :white
    @started_at = nil
    @finished_at = nil
  end

  def <<(sibling)
    @siblings << sibling
  end
end

if $PROGRAM_NAME == __FILE__
  n = gets.to_i

  temp = Hash.new {|hash, id| hash[id] = Node.new(id) }

  n.times do
    id, _, *sibling_ids = gets.chomp.split.map(&:to_i)

    node = temp[id]

    sibling_ids.each do |sibling_id|
      node << temp[sibling_id]
    end
  end

  nodes = temp.values.sort_by {|node| node.id }

  time = 0

  nodes.each do |node|
    next unless node.state == :white

    stack = [node]

    node.state == :gray

    time += 1
    node.started_at = time

    until stack.empty?
      current = stack.last
      sibling = current.siblings.sort_by(&:id).find {|s| s.state == :white }

      if sibling
        sibling.state == :gray
        time += 1
        sibling.started_at = time
        stack << sibling
      else
        stack.pop
        current.state = :black
        time += 1
        current.finished_at = time
      end
    end
  end

  nodes.each do |node|
    puts [node.id, node.started_at, node.finished_at].join(' ')
  end
end