function main(_stdin) {
    const stdin = _stdin.split('\n');
    const count = parseInt(stdin.shift(), 10);
    const stack = [];
    const colors = [''];
    const stamps = [];
    const way = [];

    for (let i = 1; i < 1 + count; i++) {
        colors[i] = 'white';
    }

    for (let i = 1; i < 1 + count; i++) {
        stamps[i] = [];
    }

    for (let i = 1; i < 1 + count; i++) {
        for (let ij = 1; ij < 1 + count; ij++) {
            if (!way[i]) way[i] = [];
            way[i][ij] = 0;
        }
    }

    const data = stdin.filter((e, i) => i < count).map(
        e => e.split(' ').map(e => parseInt(e, 10)));

    dfs(stack, data, data[0][0], colors, 1, stamps, way);
    stamps.shift();
    for (let i = 0, l = stamps.length; i < l; i++) {
        console.log(i+1, stamps[i][0], stamps[i][1]);
    }
}

function dfs(stack, data, key, colors, time, stamps, way) {
    stack.push(key);
    colors[key] = 'gray';
    stamps[key][0] = time++;

    while (stack.length) {
        key = stack[stack.length - 1];
        const d = data.find(e => e[0] === key);
        const next = getNext(d, way);
        if (next) {
            if (colors[next] === 'white') {
                colors[next] = 'gray';
                stamps[next][0] = time++;
                stack.push(next);
            }
        } else {
            const last = stack.pop();
            colors[last] = 'black';
            stamps[last][1] = time++;
        }
    }
    const untouch = stamps.findIndex((e, i) => i > 0 && !e.length);
    if(untouch > 0){
        dfs(stack, data, untouch, colors, time, stamps, way);
    }
}

function getNext(data, way) {
    const children = data.slice(2);
    if (children.length) {
        for (let child of children) {
            if (way[data[0]][child] === 0) {
                way[data[0]][child] = 1;
                return child;
            }
        }
    } else {
        return null;
    }
}



main(require('fs').readFileSync('/dev/stdin', 'utf8'));


