# coding: utf-8
# Your code here!

n = int(input())
M = []
for i in range(n):
    adj = list(map(int,input().split()))
    if adj[1] == 0:
        M += [[]]
    else:
        M += [adj[2:]]
time = 1
ans = [[j+1,0,0] for j in range(n)] # id d f

while True:
    for i in range(n):
        if ans[i][1] == 0:
            stack = [i]
            ans[i][1] = time
            time += 1
            break
        elif i == n-1:
            for k in range(n):
                print(*ans[k])
            exit()
    while stack != []:
        u = stack[-1]
        if M[u] != []:
            for i in range(len(M[u])):
                v = M[u][0]
                M[u].remove(v)
                if ans[v-1][1] == 0:
                    ans[v-1][1] = time
                    stack.append(v-1)
                    time += 1
                    break
        else:
            stack.pop()
            ans[u][2] = time
            time += 1
     
        
    
 n = int(input()) n int(input()) int input() input M = [] M [] for i in range(n):
    adj = list(map(int,input().split()))
    if adj[1] == 0:
        M += [[]]
    else:
        M += [adj[2:]] i range(n) range n adj = list(map(int,input().split())) adj list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split if adj[1] == 0:
        M += [[]]
    else:
        M += [adj[2:]] adj[1] == 0 adj[1] adj 1 0 M += [[]] M [[]] [] M += [adj[2:]] M [adj[2:]] adj[2:] adj 2: 2 time = 1 time 1 [j+1,0,0] for j in range(n) for j in range(n) j range(n) range n for j in range(n) [j+1,0,0] j+1 j 1 0 0 ans = [[j+1,0,0] for j in range(n)] ans [[j+1,0,0] for j in range(n)] while True:
    for i in range(n):
        if ans[i][1] == 0:
            stack = [i]
            ans[i][1] = time
            time += 1
            break
        elif i == n-1:
            for k in range(n):
                print(*ans[k])
            exit()
    while stack != []:
        u = stack[-1]
        if M[u] != []:
            for i in range(len(M[u])):
                v = M[u][0]
                M[u].remove(v)
                if ans[v-1][1] == 0:
                    ans[v-1][1] = time
                    stack.append(v-1)
                    time += 1
                    break
        else:
            stack.pop()
            ans[u][2] = time
            time += 1
     
        
     True for i in range(n):
        if ans[i][1] == 0:
            stack = [i]
            ans[i][1] = time
            time += 1
            break
        elif i == n-1:
            for k in range(n):
                print(*ans[k])
            exit()
     i range(n) range n if ans[i][1] == 0:
            stack = [i]
            ans[i][1] = time
            time += 1
            break
        elif i == n-1:
            for k in range(n):
                print(*ans[k])
            exit()
     ans[i][1] == 0 ans[i][1] [i] ans i 1 0 stack = [i] stack [i] i ans[i][1] = time ans[i][1] [i] ans i 1 time time += 1 time 1 break elif i == n-1:
            for k in range(n):
                print(*ans[k])
            exit()
     i == n-1 i n-1 n 1 for k in range(n):
                print(*ans[k])
             k range(n) range n print(*ans[k]) print *ans[k] ans[k] ans k exit() exit while stack != []:
        u = stack[-1]
        if M[u] != []:
            for i in range(len(M[u])):
                v = M[u][0]
                M[u].remove(v)
                if ans[v-1][1] == 0:
                    ans[v-1][1] = time
                    stack.append(v-1)
                    time += 1
                    break
        else:
            stack.pop()
            ans[u][2] = time
            time += 1
     
        
     stack != [] stack [] u = stack[-1] u stack[-1] stack -1 if M[u] != []:
            for i in range(len(M[u])):
                v = M[u][0]
                M[u].remove(v)
                if ans[v-1][1] == 0:
                    ans[v-1][1] = time
                    stack.append(v-1)
                    time += 1
                    break
        else:
            stack.pop()
            ans[u][2] = time
            time += 1
     
        
     M[u] != [] M[u] M u [] for i in range(len(M[u])):
                v = M[u][0]
                M[u].remove(v)
                if ans[v-1][1] == 0:
                    ans[v-1][1] = time
                    stack.append(v-1)
                    time += 1
                    break
         i range(len(M[u])) range len(M[u]) len M[u] M u v = M[u][0] v M[u][0] [u] M u 0 M[u].remove(v) [u].remove [u] M u remove v if ans[v-1][1] == 0:
                    ans[v-1][1] = time
                    stack.append(v-1)
                    time += 1
                    break
         ans[v-1][1] == 0 ans[v-1][1] [v-1] ans v-1 v 1 1 0 ans[v-1][1] = time ans[v-1][1] [v-1] ans v-1 v 1 1 time stack.append(v-1) stack.append stack append v-1 v 1 time += 1 time 1 break stack.pop() stack.pop stack pop ans[u][2] = time ans[u][2] [u] ans u 2 time time += 1 time 1 ans = [[j+1,0,0] for j in range(n)] [[j+1,0,0] for j in range(n)] ans time += 1 1 time adj = list(map(int,input().split())) list(map(int,input().split())) adj v = M[u][0] M[u][0] v n = int(input()) int(input()) n stack = [i] [i] stack time = 1 1 time time += 1 1 time u = stack[-1] stack[-1] u M = [] [] M time += 1 1 time M += [adj[2:]] [adj[2:]] M M += [[]] [[]] M
# coding: utf-8
# Your code here!

n = int(input())
M = []
for i in range(n):
    adj = list(map(int,input().split()))
    if adj[1] == 0:
        M += [[]]
    else:
        M += [adj[2:]]
time = 1
ans = [[j+1,0,0] for j in range(n)] # id d f

while True:
    for i in range(n):
        if ans[i][1] == 0:
            stack = [i]
            ans[i][1] = time
            time += 1
            break
        elif i == n-1:
            for k in range(n):
                print(*ans[k])
            exit()
    while stack != []:
        u = stack[-1]
        if M[u] != []:
            for i in range(len(M[u])):
                v = M[u][0]
                M[u].remove(v)
                if ans[v-1][1] == 0:
                    ans[v-1][1] = time
                    stack.append(v-1)
                    time += 1
                    break
        else:
            stack.pop()
            ans[u][2] = time
            time += 1
     
        
    

