import scala.io.StdIn.{readInt, readLine};
object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val ig = new IntGraph(n);
        (1 to n).foreach { i =>
            readLine.split(" ").drop(2).map(_.toInt).foreach { j => ig.add(i, j)}
        }
        ig.dSearch.zipWithIndex.drop(1).foreach { e => println("%d %d %d".format(e._2, e._1(0), e._1(1)))}
    }
}
class IntGraph(n: Int) {
    val size: Int = n + 1;
    val matrix: Array[Array[Int]] = Array.ofDim[Int](size, size);
    def add(src: Int, dst: Int): Unit = matrix(src)(dst) = 1;
    
    def dSearch(): Array[Array[Int]] = {
        val status = Array.ofDim[Int](size, 2);
        def _dSearch(curr: Int, time: Int): Int = {
            status(curr)(0) = time;
            val newTime = 1 + (1 until size).foldLeft(time) { (t, i) =>
                if (matrix(curr)(i) == 1 && status(i)(0) == 0) _dSearch(i, t + 1) else t;
            }
            status(curr)(1) = newTime;
            newTime;
        }
        (1 until size).foreach { i =>
            if (status(i)(0) == 0) {
                _dSearch(i, 1);
            }
        }
        status;
    }
}
