using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aoj.ALDS.Chapter11B
{
    class Program
    {
        public static void Solve()
        {
            int n = int.Parse(Console.ReadLine());
            var nodes = new List<Node>();
            var inputs = new List<int>[n];

            // Node????????????
            for (int i = 0; i < n; i++)
            {
                inputs[i] = new List<int>();
                inputs[i].AddRange(Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse));
                nodes.Add(new Node(inputs[i][0], inputs[i][1]));
            }

            // ???Node??????Node????¨????
            for (int i = 0; i < n; i++)
            {
                var childIds = inputs[i].Skip(2).ToArray();
                foreach (var childId in childIds)
                {
                    nodes[i].Children.Add(nodes[childId - 1]);
                }
            }

            // ??±???????????¢?´¢
            int counter = 0;
            //Dfs(nodes[0], ref counter);
            
            
            do {
                var node = new Node(0, 0);
                node = nodes.Where( (t) => { return t.D <= 0; } ).FirstOrDefault();
                if (node == null) {
                    break;   
                }
                Dfs(node, ref counter);
            } while (true);

            // ??????
            foreach (var node in nodes)
            {
                Console.WriteLine("{0} {1} {2}", node.Id, node.D, node.F);
            }
        }

        /// <summary>
        /// Dfs the specified node and counter.
        /// </summary>
        /// <returns>The dfs.</returns>
        /// <param name="node">Node.</param>
        /// <param name="counter">????????????????????????0????????§?????????</param>
        public static void Dfs(Node node, ref int counter)
        {
            if (node.Visited) {
                return;
            }

            node.Visited = true;
            counter++;
            node.D = counter;

            foreach (var child in node.Children)
            {
                Dfs(child, ref counter);
            }

            counter++;
            node.F = counter;
            return;
        }

        public static void Main(string[] args)
        {
            Solve();
        }
    }

    class Node
    {
        // ???????????????
        public int Id { get; set; }

        // ????????°
        public int K { get; set; }

        // ????????????
        public int D { get; set; }

        // ????????????
        public int F { get; set; }

        // ?¨??????????????????°
        public bool Visited { get; set; }


        // ??£??\???????????????(???????????????)
        public List<Node> Children { get; set; }

        public Node(int id, int k)
        {
            Id = id;
            K = k;

            D = 0;
            F = 0;
            Visited = false;

            Children = new List<Node>();
        }

        /// <summary>
        /// ?????????????????¨?????????????????????
        /// </summary>
        public Node(int id, int d, int f)
        {
            Id = id;
            K = 0;

            D = d;
            F = f;
            Visited = true;

            Children = new List<Node>();
        }
    }
}

Measured memory_bytes: 9768