fun main(args: Array<String>) = p24()

fun p24() {
    data class Vertex(val index: Int, val neighbor: List<Int>, var visitedTime: Int = -1, var completeTime: Int = -1) {
        override fun toString(): String = "${index + 1} $visitedTime $completeTime"
    }

    val n = readLine()!!.toInt()
    val vertexes = Array(n) {
        val line = readLine()!!.split(' ').map { it.toInt() }
        Vertex(line.first() - 1, line.drop(2).map { it - 1 })
    }

    var currTime = 1

    fun dfs(index: Int) {
        val vertex = vertexes[index]
        if (vertex.visitedTime != -1) return
        vertex.visitedTime = currTime++
        vertex.neighbor.forEach { dfs(it) }
        vertex.completeTime = currTime++
    }

    (0 until n).forEach { dfs(it) }

    val answer = vertexes.joinToString(System.lineSeparator())

    println(answer)
}

