def dfs(nodes, graphs, start, departed_times, finished_times, current_time)
  departed_times[start] = current_time
  current_time += 1
  (1..nodes).each do |new_node|
    if graphs[start].include?(new_node) && departed_times[new_node].nil?
      current_time = dfs(nodes, graphs, new_node, departed_times, finished_times, current_time)
    end
  end
  finished_times[start] = current_time
  current_time + 1
end

nodes = gets.to_i
departed_times = Array.new(nodes + 1)
finished_times = Array.new(nodes + 1)
graphs = Array.new(nodes + 1) do [] end
nodes.times do |i|
  numbers = gets.chomp.split(" ").map(&:to_i)
  node = numbers[0]
  graph_number = numbers[1]
  graph_number.times do |j|
    graphs[i + 1].push(numbers[j + 2])
  end
end
current_time = 1
for i in (1..nodes) do
  current_time = dfs(nodes, graphs, i, departed_times, finished_times, current_time) if departed_times[i].nil?
end
nodes.times do |i|
  puts "#{i + 1} #{departed_times[i + 1]} #{finished_times[i + 1]}"
end