class Node
  attr_accessor :id, :nexts, :parent_id, :is_reach

  def initialize id
    @id = id.to_i
    @nexts = []
    @parent_id = -1
    @is_reach = false
  end

  def setNexts next_id
    @nexts << next_id
  end

  def setReach
    is_reach = true
  end

  def isReach
    return is_reach
  end
end

$input_map = {}
n = 0
count = 0
line_count = 0
STDIN.each_line {|line|
  if count == 0
    count += 1
    n = line.to_i
    next
  end

  hoge = 0
  array = line.split " "
  id = array[0].to_i
  $input_map[id] = Node.new id
  array.each do |num|
    hoge += 1
    if hoge==1
      next
    elsif hoge==2
      next
    end

    $input_map[id].setNexts num.to_i
  end

  line_count += 1
}

$visited_list = Array.new n + 1
$visited_list.fill (false)
$discover_list = Array.new n + 1
$complete_list = Array.new n + 1
def PrintNode id, time
  $visited_list[id] = true
  $discover_list[id] = time

  if $input_map[id].nexts.empty?
    $complete_list[id] = time + 1
    return $complete_list[id]
  end

  result = 0
  $input_map[id].nexts.each do |child_id|
    if !$visited_list[child_id]
      time += 1
      result = PrintNode child_id, time
      time = time + (result - time)
    end
  end

  end_time = result == 0 ? time + 1 : result + 1

  $complete_list[id] = end_time
  return $complete_list[id]
end

index = 1
$visited_list.each do |i|
  if ($input_map.has_key? (index)) && (!$visited_list[index])
    PrintNode index, 1
  end
  index += 1
end

n.times do |i|
  print i + 1, " ", $discover_list[i + 1], " ", $complete_list[i + 1], "\n"
end

