Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 1
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	g := graph{}
	g.ee = make([]Edges, n)
	g.visited = make([]bool, n)
	g.found = make([]int, n)
	g.leave = make([]int, n)
	for i := 0; i < n; i++ {
		u := getNextInt(scanner) - 1
		k := getNextInt(scanner)
		for j := 0; j < k; j++ {
			v := getNextInt(scanner) - 1
			g.ee[u] = append(g.ee[u], Edge{
				to: v,
				id: u,
			})
		}
	}
	for i := 0; i < n; i++ {
		sort.Sort(g.ee[i])
	}
	for i := 0; i < n; i++ {
		if g.visited[i] {
			continue
		}
		g.dfs(i)
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d %d", i+1, g.found[i], g.leave[i]))
	}
}

// Edge ...
type Edge struct {
	to, id int
}

type Edges []Edge

func (a Edges) Len() int           { return len(a) }
func (a Edges) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Edges) Less(i, j int) bool { return a[i].id < a[j].id }

type graph struct {
	ee           []Edges
	t            int
	n            int
	visited      []bool
	found, leave []int
}

func (g *graph) dfs(i int) {
	g.visited[i] = true
	g.t++
	g.found[i] = g.t
	for _, e := range g.ee[i] {
		if g.visited[e.to] {
			continue
		}
		g.dfs(e.to)
	}
	g.t++
	g.leave[i] = g.t
}

C++: #include <cstdint>
#include <vector>

using namespace std;

#define GRAPH_SIZE 101
#define START_INDEX 1

enum color_t {
  WHITE,
  GRAY,
  BLACK,
};

static uint32_t t = 0;
static color_t state[GRAPH_SIZE];
static vector<uint32_t> graph[GRAPH_SIZE];

void depth_first_search(void (*visited)(uint32_t), void (*invested)(uint32_t))
{
  for (uint32_t i = START_INDEX; i < GRAPH_SIZE; i++) {
    state[i] = WHITE;
  }

  static vector<pair<uint32_t, vector<uint32_t>>> stack;

  stack.push_back(make_pair(START_INDEX, graph[START_INDEX]));
  state[START_INDEX] = GRAY;
  t++;
  visited(START_INDEX);

  while (!stack.empty()) {
    pair<uint32_t, vector<uint32_t>> top = stack.back();
    bool found = false;
    for (uint32_t i = 0; i < top.second.size(); i++) {
      uint32_t next = top.second[i];
      if (state[next] == WHITE) {
        found = true;
        state[next] = GRAY;
        t++;
        visited(next);
        stack.push_back(make_pair(next, graph[next]));
        break;
      }
    }

    if (!found) {
      stack.pop_back();
      t++;
      state[top.first] = BLACK;
      invested(top.first);
    }
  }
}

#include <iostream>

static uint32_t visited_time[GRAPH_SIZE];
static uint32_t invested_time[GRAPH_SIZE];

static void visited(uint32_t i)
{
  visited_time[i] = t;
}

static void invested(uint32_t i)
{
  invested_time[i] = t;
}

int32_t main(void)
{
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t index, len;
    cin >> index >> len;
    for (uint32_t j = 0; j < len; j++) {
      uint32_t val;
      cin >> val;
      graph[index].push_back(val);
    }
  }

  depth_first_search(visited, invested);

  for (uint32_t i = START_INDEX; i < (START_INDEX + num); i++) {
    cout << i << " " << visited_time[i] << " " << invested_time[i] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <stack>

using namespace std;

#define MAX 110
int num;
int matrix[MAX][MAX];
int dst[MAX];

void input(){
    cin >> num;
    for(int i=0;i<MAX;i++){
        dst[i] = -1;
        for(int j=0;j<MAX;j++){
            matrix[i][j] = 0;
        }
    }
    for(int i=1;i<=num;i++){
        int v; cin >> v;
        int tmp; cin >>tmp;
        for(int j=0;j<tmp;j++){
            int vi; cin >> vi;
            matrix[v-1][vi-1] = 1;
        }
    }
}

void dfs(){
    stack<int> S;
    S.push(0);
    dst[0] = 0;
    while(!S.empty()){
        int cur = S.top();
        S.pop();
        if(dst[cur]==-1 || dst[cur]== 0){
            for(int i=0;i<num;i++){
                if(matrix[cur][i]==1){
                    S.push(i);
                    dst[i] = dst[cur] + 1;
                }
            }
        }
        else if(dst[S.top()]+1 < dst[cur]){
            for(int i=0;i<num;i++){
                if(matrix[cur][i]==1){
                    S.push(i);
                    dst[i] = dst[cur] + 1;
                }
            }
        }
    }
    for(int i=0;i<num;i++){
        cout << i+1 << " " <<  dst[i] << endl;
    }
}

int main(){
    input();
    dfs();
}

C++: #include<iostream>
int main(){

  int n,a;
  std::cin>>n;

  int Adj[100][100],adj[100][100]={0},find[100][2],back[100][100],depth[100]={0},endflag[100]={0},flag;
	int u[n],k[n],kcou=0,timenow=0,v,pred,prev,firstv,backcount[100]={0};
  for(int i=0;i<n;i++){
    std::cin>>u[i];
    std::cin>>k[i];
    for(int j=0;j<k[i];j++){
      std::cin>>Adj[i][j];
    }
  }
	
	/*
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
    	std::cout<<Adj[i][j]<<" ";
    }
  	std::cout<<"\n";
  }*/
	
	v=0;
	firstv=0;
	while(timenow<n*2){
		flag=0;
		if(find[firstv][0]!=NULL&&find[firstv][1]!=NULL){
			for(int i=0;i<n;i++){
				if(find[i][0]==NULL){
					//std::cout<<i<<"\n";
					firstv=i;
					v=i;
					break;
				}
			}
		}
		
		//std::cout<<v+1<<" ";
		///std::cout<<find[v][0]<<" "<<find[v][1];
		
		if(find[v][0]==NULL){
			timenow++;
			find[v][0]=timenow;
			//std::cout<<" '"<<timenow<<"' ";
			flag=1;
			//std::cout<<flag<<" 1";
		}else if(find[v][1]==NULL){
			if(v==7){
				std::cout<<endflag[v];
				std::cout<<backcount[v];
			}
			if(endflag[v]==1){
				if(backcount[v]<=1){
				timenow++;
				find[v][1]=timenow;
				//std::cout<<" '"<<timenow<<"' ";
				}
				flag=2;
			}
			//std::cout<<flag<<" 2";
		}else{
			backcount[v]--;
			v=back[v][backcount[v]];
			//backcount[v]--;
			flag=3;
			//std::cout<<flag<<" 0";
		}

		if(flag==0){
			pred=depth[v];
			depth[v]++;
			prev=v;
			v=Adj[v][pred]-1;
			back[v][backcount[v]]=prev;
			backcount[v]++;
			//std::cout<<flag<<"\n";
		}else if(flag==1){
			if(endflag[v]==0){
				if(Adj[v][depth[v]]-1!=v&&Adj[v][depth[v]]-1!=firstv&&back[v][backcount[v]-1]!=Adj[v][depth[v]]-1){
					pred=depth[v];
					depth[v]++;
					prev=v;
					v=Adj[v][pred]-1;
					back[v][backcount[v]]=prev;
					backcount[v]++;
					//std::cout<<v<<"\n";
				}else{
					pred=depth[v];
					depth[v]++;
				}
			}
		}else if(flag==2){
			backcount[v]--;
			v=back[v][backcount[v]];
			//backcount[v]--;
			//std::cout<<flag<<"\n";
		}
		if(depth[v]>k[v]-1){
			endflag[v]=1;
		}
		
	}
	
	
	
	
  for(int i=0;i<n;i++){
     std::cout<<i+1<<" "<<find[i][0]<<" ";
     std::cout<<find[i][1]<<"\n";
    //std::cout<<"\n";
  }

  return 0;
}