function Main(input) {
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});
    var n = parseInt(input[0][0], 10);
    var line;
    arr_time = Array.from({length: n}, () => [0, 0, 0]);
    arr_v = [];
    for (var i = 0; i < n; i++){
        line = input[i + 1].map(e => parseInt(e, 10));
        arr_v.push([]);
        for (var j = 2; j < line.length; j++){
            arr_v[i].push(line[j]);
        }
    }
    var tmp = 0;
    time = 1;
    history = [];
    whole_loop:
    while (time < n * 2){
        for (var i = tmp; i < n; i++){
            if (arr_time[i][1] === 0) {
                arr_time[i] = [i + 1, time, 0];
                history = [i];
                time ++;
                finished = DFS(i);
                tmp = i;
                continue whole_loop;
            }
        }
    }
    for (var x of arr_time){
        console.log(x.join(" "))
    }
}

function DFS(tmpNode){
    for (let x of arr_v[tmpNode]){
        if (arr_time[x - 1][1] === 0) {
            arr_time[x - 1] = [x, time, 0];
            time ++;
            history.push(x);
            DFS(x - 1);
        } 
    }
    arr_time[tmpNode][2] = time;
    time ++;
    history.pop();
    return true;
}

Main(require("fs").readFileSync("/dev/stdin", "utf8")); 

