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
}

Python: NOT_FOUND = 0
FOUND = 1
FINISHED = 2


class Stack(object):
    def __init__(self):
        self._stack = []

    def push(self, val):
        self._stack.append(val)

    def pop(self):
        val = self._stack[-1]
        del self._stack[-1]
        return val

    def top(self):
        if len(self._stack) > 0:
            return self._stack[-1]
        else:
            return None

    def is_stacked(self, val):
        if len(self._stack) > 0:
            return val in self._stack
        else:
            return None

    def get_len(self):
        return len(self._stack)


def parse_line(line):
    vals = [int(v) for v in line.split(' ')]
    node, n_node, nodes = vals[0], vals[1], vals[2:]
    # convert zero origin
    nodes = [n - 1 for n in nodes]
    return node, n_node, nodes


def main():
    # get input
    n = int(input().strip())
    graph = []
    for _ in range(n):
        line = input().strip()
        _, _, nodes = parse_line(line)
        graph.append(nodes)

    # initialize
    stack = Stack()
    stats = [NOT_FOUND for _ in range(n)]
    found_times = [None for _ in range(n)]
    finished_times = [None for _ in range(n)]
    counter = 0

    # dfs
    while sum(stats) < n * FINISHED:
        counter += 1
        init_node = None
        for node in range(n):
            if stats[node] == NOT_FOUND:
                init_node = node
                break
        assert init_node is not None
        stack.push(init_node)
        stats[init_node] = FOUND
        found_times[init_node] = counter

        while stack.get_len() > 0:
            counter += 1
            crr_node = stack.top()
            adj_nodes = graph[crr_node]
            for adj_node in adj_nodes:
                # exist not stacked node
                if stats[adj_node] == NOT_FOUND:
                    found_times[adj_node] = counter
                    stats[adj_node] = FOUND
                    stack.push(adj_node)
                    crr_node = adj_node
                    break
            else:
                # not exist not stacked node
                finished_times[crr_node] = counter
                stats[crr_node] = FINISHED
                stack.pop()
                crr_node = stack.top()

    for i in range(n):
        print('{} {} {}'.format(i + 1, found_times[i], finished_times[i]))


if __name__ == '__main__':
    main()

Python: import sys
input = sys.stdin.readline
from collections import deque

n = int(input())
graph = {i: deque() for i in range(1, n+1)}
for _ in range(n):
    u, k, *v = [int(x) for x in input().split()]
    v.sort()
    for i in v:
        graph[u].append(i)
        #graph[i].append(u)

seen = [0]*(n+1)
stack = []
time = 0
seentime = [0]*(n+1)
donetime = [0]*(n+1)

def dfs(j):
    global time
    seen[j] = 1
    time += 1
    seentime[j] = time
    stack.append(j)
    while stack:
        s = stack[-1]
        if not graph[s]:
            stack.pop()
            time += 1
            donetime[s] = time
            continue
        g_NO  = graph[s].popleft()
        if seen[g_NO]:
            continue
        seen[g_NO] = 1
        stack.append(g_NO)
        time += 1
        seentime[g_NO] = time

for j in range(1, n+1):
    if seen[j]:
        continue
    dfs(j)

for k, a, b in zip(range(1, n+1), seentime[1:], donetime[1:]):
    print(k, a, b)

Python: n = int(input())
g = []
for i in range(n):
    a = list(map(int,input().split()))
    g.append(a[2:])
d = [0]*n
f = [0]*n
global t
t = 0

def find(x):
    global t
    if len(g[x-1]) == 0:
        t += 1
        f[x-1] = t
    else:
        for i in g[x-1]:
            if d[i-1] == 0:
                t += 1
                d[i-1] = t
                find(i)
        t += 1
        f[x-1] = t

for i in range(n):
    if d[i] == 0:
        t += 1
        d[i] = t
        find(i+1)
for i in range(n):
    print(i+1, d[i], f[i])

Python: n=0
t=1
def adjacent_matrix():
    global n
    n=int(input())
    v=[]
    array=[]
    for i in range(n):
        zero=[0 for i in range(n)]
        a=list(map(int,input().split()))
        a=a[2:]
        for j in a:
            zero[j-1]=1
        array.append(zero)
    return array
def dfs(array,i,d,f):
    global t
    if i == n:
        return
    else:
        if(d[i] == 0):
            d[i] = t
            t += 1
            for (j,x) in enumerate(array[i]):
                if x == 1:
                    dfs(array,j,d,f)
            f[i] = t
            t += 1
    return 


def main():
    array=adjacent_matrix()
    print()
    d=[0 for i in range(n)]
    f=[0 for i in range(n)]
    for i in range(n):
        dfs(array,i,d,f)
    for i in range(n):
        print("{} {} {} ".format(i+1,d[i],f[i]))

if __name__ == "__main__":
    main()