def fx(nx,i,d,f,count)
  if d[i]==0 then
    d[i]=count
    count+=1
  end
  for k in nx[i] do
    count=fx(nx,k,d,f,count)
  end
  if f[i]==0 then
    f[i]=count
    count+=1
  end
  return count
end


n=gets.to_i;
a=Array.new(n){|i|i+1}
d=Array.new(n,0)
f=Array.new(n,0)
x=Array.new(n){Array.new}

m=Array.new
n.times do |i|
  m.clear
  m=gets.split.map{|i|i.to_i}
  m[1].times do |j|
    x[m[0]-1] << m[j+2]-1
  end
end
fx(x,0,d,f,1)
n.times do |i|
  print a[i]," ",d[i]," ",f[i],"\n"
end
