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

object Main {
  def main(args:Array[String]) = {
    val n = readInt
    val d = new Array[Int](n)
    val f = new Array[Int](n)

    val g = {
      for(i<-0 until n) yield {
        val v = readLine.split(" ").map(_.toInt)
        (for(j<-2 until 2+v(1)) yield v(j) ).toArray
      }
    }.toArray

    def scan(i:Int,cnt:Int):Int = {
      if(d(i)==0) d(i) = cnt

      if(g(i).length > 0) {
        var cnt2 = cnt
        for(j<-0 until g(i).length) {
          if(f(g(i)(j)-1)==0 && d(g(i)(j)-1)==0) {
            cnt2 = scan(g(i)(j)-1,cnt2+1)
          }
        }
        f(i) = cnt2 +1
        cnt2 +1
      } else {
        f(i) = cnt+1
        cnt+1
      }
    }

    var cnt = 1
    for(i<-0 until n) {
      if(f(i)==0) cnt = scan(i,cnt)+1
    }

    for(i<-0 until n) {
      println((i+1) + " " + d(i) + " " + f(i))
    }
  }
}