N = gets.to_i
$adjacency_list = []
N.times{
  u, k, *v = gets.split.map &:to_i
  $adjacency_list[u] = v
}
$d = Array.new(N)
$f = Array.new(N)
$time = 1
def record_time v
  unless $d[v]
    $d[v] = $time
    $time += 1
  end
  $adjacency_list[v].each{|arr_v|
    record_time arr_v unless $d[arr_v]
  }
  unless $f[v]
    $f[v] = $time
    $time += 1
  end
end
(1..N).each{|i|
  record_time(i) unless $d[i]
}
(1..N).each{|i|
  puts "#{i} #{$d[i]} #{$f[i]}"
}