object Main extends App {
    val in = io.Source.stdin.getLines.map(_.split(" ").map(_.toInt))
    val n = in.next.head
    val tree = in.map(r => r.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
            val f_chld =
                if (ids.nonEmpty) {
                    ids.foldLeft(d)((_d, _id) => solve(_d+1, _id, tree(_id)))
                } else {
                    d
                }
            res(id)(1) = f_chld+1
            res(id)(2) = 1
            f_chld+1
        }
    }
    res.zipWithIndex.foreach{case (x, i) => println(i + 1 + " " + x.init.mkString(" "))}
}