object Main extends App {
  val n: Int = io.StdIn.readInt()
  val d: Array[Int] = Array.ofDim(n)
  val f: Array[Int] = Array.ofDim(n)
  val adjacencyMatrix: Seq[Array[Int]] =
    for (_ <- (0 until n).toVector)
      yield io.StdIn.readLine().split(" ").drop(2).map(_.toInt - 1)
  (0 until n).foldLeft[Int](0)((acc, p) => if (d(p) == 0) loop(p, acc) else acc)
  for (i <- 0 until n) println(s"${i+1} ${d(i)} ${f(i)}")

  def loop(current: Int, time: Int): Int = {
    d(current) = time + 1
    f(current) =
      adjacencyMatrix(current).foldLeft[Int](d(current))(
        (acc, p) => if (d(p) == 0) loop(p, acc) else acc
      ) + 1
    f(current)
  }
}
