let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let dfs vs n =
  let d = Array.make (n + 1) 0 in
  let f = Array.make (n + 1) 0 in
  let time = ref 1 in
  let rec doit i =
    d.(i) <- !time;
    incr time;
    List.iter (fun e -> if d.(e) = 0 then doit e) vs.(i);
    f.(i) <- !time;
    incr time in
  for i = 1 to n do
    if d.(i) <> 0 then ()
    else
      doit i;
  done;
  (d, f)

let () =
  let n = read_int () in
  let vs = Array.make (n + 1) [] in
  for _ = 0 to n - 1 do
    match read_line () |> split_on_char ' ' |> List.map int_of_string with
    | u :: _ :: l -> vs.(u) <- l
    | _ -> assert false
  done;
  let (d, f) = dfs vs n in
  for i = 1 to n do
    Printf.printf "%d %d %d\n" i d.(i) f.(i)
  done