import scala.io.StdIn.{readInt, readLine};

object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt
        val matrix = Array.ofDim[Int](n + 1, n + 1);
        for (i <- (1 to n)) {
            val line = readLine.split(" ").map(_.toInt);
            line.slice(2, line.length).foreach { j => matrix(i)(j) = 1}
        }
        val ret = dfs(matrix, 1);
        (1 to n).foreach { i =>
            println("%d %d %d".format(i, ret(i)(0), ret(i)(1)));
        }
    }
    
    def dfs(matrix: Array[Array[Int]], start: Int): Array[Array[Int]] = {
        val len = matrix.length;
        val status = Array.ofDim[Int](len, 2);
        def go(curr: Int, now: Int): Int = {
            status(curr)(0) = now;
            val next = (1 until len).foldLeft(now + 1) { (t, i) => 
                    if (status(i)(0) == 0 && matrix(curr)(i) == 1) go(i, t) else t
                }
            status(curr)(1) = next;
            next + 1;
        }
        go(1, 1);
        status;
    }
}
