import java.util.*

var d = IntArray(0)
var f = IntArray(0)
var count = 0
var map =java.util.HashMap<Int, Array<Int>>()
fun main(args: Array<String>) {


    var sc = Scanner(System.`in`)

    val n = sc.nextInt()

     d = IntArray(n + 1)
     f = IntArray(n + 1)


    for(i in 0 until n){
        val id = sc.nextInt()
        val k = sc.nextInt()

        map[id] = (0 until k).map { sc.nextInt() }.toIntArray().toTypedArray()
    }

    for(i in 1..n){
        ds(i)
    }

    for(i in 1..n){
        println("$i ${d[i]} ${f[i]}")
    }

}

private fun ds(id: Int){
    val neighbors = map[id]!!
    if(d[id] > 0){
        return
    }
    d[id] = ++count
    for(i in neighbors){
        ds(i)
    }
    f[id] = ++count
}



