n = gets.to_i
d = Array.new n
f = Array.new n
v = Array.new n
g = Array.new n
n.times do
  u, _, *cs = gets.split.map(&:to_i)
  g[u-1] = cs.map {|c| c - 1 }
end
t = 1
dfs = lambda do |u|
  break if v[u]
  v[u] = true
  d[u] = t
  t += 1
  g[u].each do |c|
    dfs[c]
  end
  f[u] = t
  t += 1
end
n.times do |u|
  dfs[u]
end
n.times do |u|
  puts "#{u+1} #{d[u]} #{f[u]}"
end