g = []

n = STDIN.gets.to_i
n.times.each {
  as = STDIN.gets.split(' ').map{|x| x.to_i }
  g[as[0]] = as.drop(2)
}

stack = []
mode = [] # nil: ????¨????, 1: ?¨??????????, 2: ??£??\?????¨????¨??????????
start_t = []
end_t = []

t = 1
v = 1
start_t[v] = t
stack.push(v)
while stack.size != 0
  t = t + 1
  adjacencies = g[v]
  has_yet = false
  for a in adjacencies
    if mode[a] == nil
      # ????¨????
      mode[a] = 1
      v = a
      start_t[v] = t
      stack.push(v)
      has_yet = true
      break
    end
  end
  if !has_yet
    # ??£??\??????????????¨??¨?¨??????????
    mode[v] = 2
    end_t[v] = t
    stack.pop
    v = stack.last
  end
end

for i in 1..n
  puts "#{i} #{start_t[i]} #{end_t[i]}"
end