import sys, math
from itertools import permutations, combinations, accumulate
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

N = int(input())
G = [list(map(int, input().split())) for _ in range(N)]
c = [0]*N # 0:not found, 1:found, 2:finished
res = [[0]*2 for _ in range(N)]

time = 1
def dfs(index):
#    print('index:', index)
    global time
    if c[index]==0:
        c[index]=1
        res[index][0] = time # found
        time += 1
    elif c[index]==1:
        return
    elif c[index]==2:
        return

    u, k, *v = G[index]
    for node in v:
        dfs(node-1) # 1-index -> 0-index

    c[index] = 2
    res[index][1] = time # finished
    time += 1
    return

for i in range(N):
    dfs(i)
res = [[val1]+val2 for val1, val2 in zip(list(range(1, N+1)), res)]
for val in res:
    print(*val)
 import sys, math sys math from itertools import permutations, combinations, accumulate from collections import defaultdict, Counter, deque from math import factorial from bisect import bisect_left sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 enu = enumerate enu enumerate MOD = 10**9+7 MOD 10**9+7 10**9 10 9 7 def input(): return sys.stdin.readline()[:-1] input def pri(x): print('\n'.join(map(str, x))) pri x x N = int(input()) N int(input()) int input() input list(map(int, input().split())) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split G = [list(map(int, input().split())) for _ in range(N)] G [list(map(int, input().split())) for _ in range(N)] c = [0]*N c [0]*N [0] 0 N [0]*2 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0]*2 [0] 0 2 res = [[0]*2 for _ in range(N)] res [[0]*2 for _ in range(N)] time = 1 time 1 def dfs(index):
#    print('index:', index)
    global time
    if c[index]==0:
        c[index]=1
        res[index][0] = time # found
        time += 1
    elif c[index]==1:
        return
    elif c[index]==2:
        return

    u, k, *v = G[index]
    for node in v:
        dfs(node-1) # 1-index -> 0-index

    c[index] = 2
    res[index][1] = time # finished
    time += 1
    return dfs global time if c[index]==0:
        c[index]=1
        res[index][0] = time # found
        time += 1
    elif c[index]==1:
        return
    elif c[index]==2:
        return

     c[index]==0 c[index] c index 0 c[index]=1 c[index] c index 1 res[index][0] = time res[index][0] [index] res index 0 time time += 1 time 1 elif c[index]==1:
        return
     c[index]==1 c[index] c index 1 return elif c[index]==2:
        return

     c[index]==2 c[index] c index 2 return u, k, *v = G[index] u G[index] G index k *v v for node in v:
        dfs(node-1) # 1-index -> 0-index

     node v dfs(node-1) dfs node-1 node 1 c[index] = 2 c[index] c index 2 res[index][1] = time res[index][1] [index] res index 1 time time += 1 time 1 return index index for i in range(N):
    dfs(i) i range(N) range N dfs(i) dfs i [val1]+val2 for val1, val2 in zip(list(range(1, N+1)), res) for val1, val2 in zip(list(range(1, N+1)), res) val1 val2 zip(list(range(1, N+1)), res) zip list(range(1, N+1)) list range(1, N+1) range 1 N+1 N 1 res for val1, val2 in zip(list(range(1, N+1)), res) [val1]+val2 [val1] val1 val2 res = [[val1]+val2 for val1, val2 in zip(list(range(1, N+1)), res)] res [[val1]+val2 for val1, val2 in zip(list(range(1, N+1)), res)] for val in res:
    print(*val) val res print(*val) print *val val G = [list(map(int, input().split())) for _ in range(N)] [list(map(int, input().split())) for _ in range(N)] G res = [[val1]+val2 for val1, val2 in zip(list(range(1, N+1)), res)] [[val1]+val2 for val1, val2 in zip(list(range(1, N+1)), res)] res c = [0]*N [0]*N c time = 1 1 time def dfs(index):
#    print('index:', index)
    global time
    if c[index]==0:
        c[index]=1
        res[index][0] = time # found
        time += 1
    elif c[index]==1:
        return
    elif c[index]==2:
        return

    u, k, *v = G[index]
    for node in v:
        dfs(node-1) # 1-index -> 0-index

    c[index] = 2
    res[index][1] = time # finished
    time += 1
    return def dfs(index):
#    print('index:', index)
    global time
    if c[index]==0:
        c[index]=1
        res[index][0] = time # found
        time += 1
    elif c[index]==1:
        return
    elif c[index]==2:
        return

    u, k, *v = G[index]
    for node in v:
        dfs(node-1) # 1-index -> 0-index

    c[index] = 2
    res[index][1] = time # finished
    time += 1
    return dfs res = [[0]*2 for _ in range(N)] [[0]*2 for _ in range(N)] res MOD = 10**9+7 10**9+7 MOD def pri(x): print('\n'.join(map(str, x))) def pri(x): print('\n'.join(map(str, x))) pri N = int(input()) int(input()) N enu = enumerate enumerate enu def input(): return sys.stdin.readline()[:-1] def input(): return sys.stdin.readline()[:-1] input
import sys, math
from itertools import permutations, combinations, accumulate
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

N = int(input())
G = [list(map(int, input().split())) for _ in range(N)]
c = [0]*N # 0:not found, 1:found, 2:finished
res = [[0]*2 for _ in range(N)]

time = 1
def dfs(index):
#    print('index:', index)
    global time
    if c[index]==0:
        c[index]=1
        res[index][0] = time # found
        time += 1
    elif c[index]==1:
        return
    elif c[index]==2:
        return

    u, k, *v = G[index]
    for node in v:
        dfs(node-1) # 1-index -> 0-index

    c[index] = 2
    res[index][1] = time # finished
    time += 1
    return

for i in range(N):
    dfs(i)
res = [[val1]+val2 for val1, val2 in zip(list(range(1, N+1)), res)]
for val in res:
    print(*val)

