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