n = gets.chomp.to_i
$l = Array.new(n+1) { Array.new }
1.upto(n) do |i|
 arr = gets.chomp.split.map(&:to_i)
 arr.shift
 arr.shift
 $l[i] = arr
end

$d = Array.new(n+1, nil)
$f = Array.new(n+1, nil)

$i = 0

def search(t)
  return if $d[t]
  $i += 1
  $d[t] = $i
  $l[t].each do |l|
    search(l)
  end
  $i += 1
  $f[t] = $i
end

1.upto(n) do |i|
  search(i) unless $d[i]
end

1.upto(n) do |i|
  puts "#{i} #{$d[i]} #{$f[i]}"
end


