Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type TimeRecord struct {
	discoverTime int
	finishTime   int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	timeRecordMap := make([]TimeRecord, n)
	childrenMap := make([][]int, n)
	for i := 1; i <= n; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		timeRecordMap[node-1] = TimeRecord{-1, -1}

		childNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		childrenMap[node-1] = make([]int, childNum)
		for j := 0; j < childNum; j++ {
			child, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			childrenMap[node-1][j] = child
		}
	}

	timer := 1
	for {
		startNode := findStartNode(childrenMap, timeRecordMap)
		if startNode > 0 {
			visit(startNode, &timer, childrenMap, timeRecordMap)
		} else {
			break
		}
	}
	for k, v := range timeRecordMap {
		fmt.Printf("%d %d %d\n", k+1, v.discoverTime, v.finishTime)
	}
}

func findStartNode(childrenMap [][]int, timeRecordMap []TimeRecord) int {
	x := -1
	for node, timeRecord := range timeRecordMap {
		if timeRecord.discoverTime < 0 {
			x = node + 1
			break
		}
	}
	if x < 0 {
		return -1
	}

	found := false
	i := 0
	for i < len(childrenMap) {
		children := childrenMap[i]
		for j := 0; j < len(children); j++ {
			if children[j] == x {
				x = j + 1
				found = true
				break
			}
		}
		if found {
			i = 0
		} else {
			i++
		}
	}
	return x
}

func visit(currentNode int, timer *int, childrenMap [][]int, timeRecordMap []TimeRecord) {
	if timeRecordMap[currentNode-1].discoverTime > 0 {
		return
	}
	children := childrenMap[currentNode-1]
	timeRecordMap[currentNode-1] = TimeRecord{*timer, timeRecordMap[currentNode-1].finishTime}
	(*timer)++
	for i := 0; i < len(children); i++ {
		visit(children[i], timer, childrenMap, timeRecordMap)
	}
	timeRecordMap[currentNode-1] = TimeRecord{timeRecordMap[currentNode-1].discoverTime, *timer}
	(*timer)++
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: /**
 * @Author: yujirotakahashi
 * @Date:   2020-05-17T20:21:41+09:00
 * @Last modified by:   yujirotakahashi
 * @Last modified time: 2020-05-22T23:42:35+09:00
 */

#include<bits/stdc++.h>
using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//const ll INF = (1LL << 60);
//const double pi=acos(-1.0);
using ll = long long;
using PAIR = pair<int, int>;
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/**************************************/

/******** User-defined Function *******/
using Graph = vector<vector<int>>;
int n;
int m[100][100];
int color[100],d[100],f[100],tt;

int nt[100];

//color while-1 Gray-2 Black-3
int next(int u){
    for(int v=nt[u]; v<n; ++v){
        nt[u] = v + 1;
        if(m[u][v]) return v;
    }
    return -1;
}

void dfs(int r){
    rep(i,n) nt[i] = 0;

    stack<int> S;
    S.push(r);
    color[r] = 1;
    d[r] = ++tt;

    while(!S.empty()){
        int u = S.top();
        int v = next(u);
        if(v != -1){
            if(color[v] == 0){
                color[v] = 1;
                d[v] = ++tt;
                S.push(v);
            }
        }else{
            S.pop();
            color[u] = 2;
            f[u] = ++tt;
        }
    }
}
/**************************************/

/********** Main()  function **********/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  rep(i,n)rep(j,n) m[i][j] = 0;

  rep(i,n){
      int u,k; cin >> u >> k;
      u--;
      rep(j,k){
          int v; cin >> v;
          v--;
          m[u][v] = 1;
      }
  }

  //dfs

  rep(i,n){
       color[i] = 0;
       nt[i] = 0;
   }
  tt = 0;

  rep(u,n){
      if(color[u] == 0) dfs(u);
  }

  //出力
  rep(i,n){
      cout << i+1 << " " << d[i] << " " << f[i] << endl;
  }

  return 0;
}
/********  Main() Ends Here *************/

C++: #include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
void func(int p);
typedef pair<int,int>P;
int n;
array<array<int,101>,101>arr;
array<P,101>arrp;
array<bool,101>arrb;
int Time;
int main(void){
	int u,k,v;
	int i,j;
	cin>>n;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			arr[i][j]=0;
		}
	}
	for(i=0;i<n;i++){
		cin>>u>>k;
		for(j=0;j<k;j++){
			cin>>v;
			arr[u][v]=1;
		}
	}
	arrb.fill(false);
	Time=0;
	for(i=1;i<=n;i++){
		if(!arrb[i]){
			func(i);
		}
	}
	for(i=1;i<=n;i++){
		cout<<i<<" "<<arrp[i].F<<" "<<arrp[i].S<<endl;
	}
	return 0;
}
void func(int p){
	int i,j;
	//cout<<p<<endl;
	arrb[p]=true;
	Time++;
	arrp[p].F=Time;
	for(i=1;i<=n;i++){
		if(arr[p][i]==1){
			if(!arrb[i]){
				func(i);
			}
		}
	}
	Time++;
	arrp[p].S=Time;
}