type node = {
        id:int;
        mutable stime:int;
        mutable etime:int;
        mutable nodes: node array
     }
;;

let new_node id = {id=id;stime=0;etime=0;nodes=[||]};;

let dfs graph =
  let total_time = ref 0 in
  let rec search nd time =
    if nd.stime > 0 then time
    else if nd.nodes == [||] then
      begin
         nd.stime <- time+1;nd.etime <- time+2;time+2
       end
    else
      begin
        nd.stime <- time+1;
        let last_time = ref (time+1) in
        for i = 0 to (Array.length nd.nodes)-1 do
          last_time := search nd.nodes.(i) !last_time
        done;
        nd.etime <- !last_time+1;!last_time+1
      end
  in
  for i = 0 to (Array.length graph)-1 do
    if graph.(i).stime == 0 then total_time := search graph.(i) !total_time
  done
;;

let () =
    let n = read_int () in
    let graph = Array.init n (fun x -> new_node (x+1)) in
    let rec read = function
        0 -> ()
      | i ->
            let (v,k) = Scanf.scanf "%d %d" (fun x y -> (x,y)) in
            if k <> 0 then
                graph.(v-1).nodes <- Array.init k (fun x ->
                         let child = Scanf.scanf " %d" (fun y -> y) in graph.(child-1))
            ;Scanf.scanf "\n" ()
            ;read (i-1)
    in
    read n;
    dfs graph;
    Array.iter (fun x -> Printf.printf "%d %d %d\n" x.id x.stime x.etime) graph
;;