n = int(input())
G = [[] for _ in range(n)]
u1 = 0
for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
    if k != 0 and u1 == 0:
        u1 = u

time = 1
u = u1
D = []
out = [[i+1, 0, 0] for i in range(n)]
T = []
while True:
    if u not in D:
        out[u - 1][1] = time
        D.append(u)
        T.append(u)

    for d in D:
        if d in G[u - 1]:
            idx = G[u - 1].index(d)
            del G[u - 1][idx]

    if G[u - 1] == []:
        time += 1
        out[T.pop() - 1][2] = time
        if T == []:
            if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                time += 1
                continue
        else:
            u = T[-1]
    else:
        u = G[u - 1].pop(0)
        time += 1

for ni in range(n):
    print(*out[ni])
 n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] G = [[] for _ in range(n)] G [[] for _ in range(n)] u1 = 0 u1 0 for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
    if k != 0 and u1 == 0:
        u1 = u _ range(n) range n int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i u, k, *v = [int(i) for i in input().split()] u [int(i) for i in input().split()] k *v v G[u - 1] = v G[u - 1] G u - 1 u 1 v if k != 0 and u1 == 0:
        u1 = u k != 0 and u1 == 0 k != 0 k 0 u1 == 0 u1 0 u1 = u u1 u time = 1 time 1 u = u1 u u1 D = [] D [] [i+1, 0, 0] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [i+1, 0, 0] i+1 i 1 0 0 out = [[i+1, 0, 0] for i in range(n)] out [[i+1, 0, 0] for i in range(n)] T = [] T [] while True:
    if u not in D:
        out[u - 1][1] = time
        D.append(u)
        T.append(u)

    for d in D:
        if d in G[u - 1]:
            idx = G[u - 1].index(d)
            del G[u - 1][idx]

    if G[u - 1] == []:
        time += 1
        out[T.pop() - 1][2] = time
        if T == []:
            if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                time += 1
                continue
        else:
            u = T[-1]
    else:
        u = G[u - 1].pop(0)
        time += 1 True if u not in D:
        out[u - 1][1] = time
        D.append(u)
        T.append(u)

     u not in D u D out[u - 1][1] = time out[u - 1][1] [u - 1] out u - 1 u 1 1 time D.append(u) D.append D append u T.append(u) T.append T append u for d in D:
        if d in G[u - 1]:
            idx = G[u - 1].index(d)
            del G[u - 1][idx]

     d D if d in G[u - 1]:
            idx = G[u - 1].index(d)
            del G[u - 1][idx]

     d in G[u - 1] d G[u - 1] G u - 1 u 1 idx = G[u - 1].index(d) idx G[u - 1].index(d) [u - 1].index [u - 1] G u - 1 u 1 index d del G[u - 1][idx] G[u - 1][idx] [u - 1] G u - 1 u 1 idx if G[u - 1] == []:
        time += 1
        out[T.pop() - 1][2] = time
        if T == []:
            if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                time += 1
                continue
        else:
            u = T[-1]
    else:
        u = G[u - 1].pop(0)
        time += 1 G[u - 1] == [] G[u - 1] G u - 1 u 1 [] time += 1 time 1 out[T.pop() - 1][2] = time out[T.pop() - 1][2] [T.pop() - 1] out T.pop() - 1 T.pop() T.pop T pop 1 2 time if T == []:
            if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                time += 1
                continue
        else:
            u = T[-1]
     T == [] T [] if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                time += 1
                continue
         len(D) == n len(D) len D n break for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                 ni range(n) range n if ni + 1 not in D:
                        u = ni + 1
                        break
                 ni + 1 not in D ni + 1 ni 1 D u = ni + 1 u ni + 1 ni 1 break time += 1 time 1 continue u = T[-1] u T[-1] T -1 u = G[u - 1].pop(0) u G[u - 1].pop(0) [u - 1].pop [u - 1] G u - 1 u 1 pop 0 time += 1 time 1 for ni in range(n):
    print(*out[ni]) ni range(n) range n print(*out[ni]) print *out[ni] out[ni] out ni u = T[-1] T[-1] u D = [] [] D u = G[u - 1].pop(0) G[u - 1].pop(0) u u = ni + 1 ni + 1 u time += 1 1 time time += 1 1 time k, *v = [int(i) for i in input().split()] [int(i) for i in input().split()] k idx = G[u - 1].index(d) G[u - 1].index(d) idx u1 = 0 0 u1 u, k, *v = [int(i) for i in input().split()] [int(i) for i in input().split()] u time = 1 1 time G = [[] for _ in range(n)] [[] for _ in range(n)] G u1 = u u u1 T = [] [] T out = [[i+1, 0, 0] for i in range(n)] [[i+1, 0, 0] for i in range(n)] out n = int(input()) int(input()) n u = u1 u1 u time += 1 1 time
n = int(input())
G = [[] for _ in range(n)]
u1 = 0
for _ in range(n):
    u, k, *v = [int(i) for i in input().split()]
    G[u - 1] = v
    if k != 0 and u1 == 0:
        u1 = u

time = 1
u = u1
D = []
out = [[i+1, 0, 0] for i in range(n)]
T = []
while True:
    if u not in D:
        out[u - 1][1] = time
        D.append(u)
        T.append(u)

    for d in D:
        if d in G[u - 1]:
            idx = G[u - 1].index(d)
            del G[u - 1][idx]

    if G[u - 1] == []:
        time += 1
        out[T.pop() - 1][2] = time
        if T == []:
            if len(D) == n:
                break
            else:
                for ni in range(n):
                    if ni + 1 not in D:
                        u = ni + 1
                        break
                time += 1
                continue
        else:
            u = T[-1]
    else:
        u = G[u - 1].pop(0)
        time += 1

for ni in range(n):
    print(*out[ni])

