var input = require('fs').readFileSync('/dev/stdin', 'utf8').trim();
var lines = input.split('\n');

var n = +lines.shift();

var g = [[]];
for (var i = 1; i <= n; i++) {
    g.push([]);
    for (var j = 1; j <= n; j++) {
        g[i][j] = 0;
    }
}

for (var i = 1; i <= n; i++) {
    var u = lines.shift().split(' ').map(function(n){return +n;});
    var id = u[0];
    var k = u[1];

    for (var j = 0; j < k; j++) {
        g[i][u[2 + j]] = 1;
    }
}

var ts = 1;
var v = [];
for (var i = 1; i <= n; i++) {
    v[i] = {d: -1, f: -1};    
}
function visit(id) {
    v[id].d = ts++;

    for (var i = 1; i <= n; i++) {
        if (g[id][i] == 1 && v[i].d == -1) {
            visit(i);
        }
    }

    v[id].f = ts++;
}
visit(1);

for (var id = 1; id <= n; id++) {
    console.log(id, v[id].d, v[id].f);
}