//config = { input: 'tmp', newline: '\r\n' }; // win
config = { input: '/dev/stdin', newline: '\n' }; // linux

line = require('fs').readFileSync(config.input, 'ascii')
  .trim()
  .split(config.newline);
n = Number(line.shift());
adj = {};
for (i in line) {
  ary = line[i].split(' ');
  j = ary.shift();
  ary.shift();
  adj[j] = ary;
}

for (i in adj)
  adj[i].sort(function (a, b) { return Number(a) - Number(b); });

d = {};
f = {};
t = 0;
cur = null;
rest = null;
stack = [];

function detect(node) {
  stack.push([cur, rest]);
  t++;
  d[node] = t;
  cur = node;
  rest = [].concat(adj[node]);
}

function finish(node) {
  var ary;
  t++;
  f[node] = t;
  ary = stack.pop();
  cur = ary[0];
  rest = ary[1];
}

function round(from) {
  var child;
  detect(from);
  while (cur) {
    while (rest.length !== 0) {
      child = rest.shift();
      if (!d.hasOwnProperty(child)) detect(child);
    }
    finish(cur);
  }
}

for (i = 1; i <= n; i++)
  if (!d.hasOwnProperty(String(i))) round(String(i));

for (i = 1; i <= n; i++)
  console.log("%d %s %s", i, d[i], f[i]);