def depth_first_search ms, visit_logs, visit_cnts, cnt
  cnt += 1
  i = visit_logs.last
  visit_cnts[i] = [cnt, nil]
  ms[i].each do |a|
    if visit_cnts[a - 1].nil?
      visit_logs << a - 1
      visit_cnts, cnt = depth_first_search(ms, visit_logs, visit_cnts, cnt)
    end
  end

  cnt += 1
  visit_logs.pop
  visit_cnts[i][1] = cnt

  return visit_cnts, cnt
end

n = gets.chomp.to_i
ms = Array.new(n)
visit_cnts = Array.new(n)
cnt = 0
n.times do
  num, degree, *adjacents = gets.chomp.split(' ').map(&:to_i)
  ms[num - 1] = adjacents
end
while true
  undone_i = visit_cnts.find_index{|d| d.nil? }
  break unless undone_i
  visit_cnts, cnt = depth_first_search ms, [undone_i], visit_cnts, cnt
end
visit_cnts.each_with_index do |l, i|
  puts "#{i + 1} #{l[0]} #{l[1]}"
end
