object Main extends App {
    val in = io.Source.stdin.getLines.map(_.split(" ").map(_.toInt))
    val n = in.next.head
    val tree = in.map(_.drop(2).map(_ - 1)).toList
    val res = List.fill(n)(Array.fill(3)(0))
    solve(1, 0, tree(0))
    def solve(d: Int, id: Int, ids: Array[Int]): Int = {
        if (res(id)(2) == 1) {
            d - 1
        } else {
            res(id)(0) = d
            res(id)(2) = 1
            val f_chld = ids.foldLeft(d)((_d, _id) => solve(_d + 1, _id, tree(_id)))
            res(id)(1) = f_chld + 1
            f_chld + 1
        }
    }
    res.zipWithIndex.foreach{case (x, i) => println(i + 1 + " " + x.init.mkString(" "))}
}