from collections import deque
n = int(input())
u = []
k = []
v = []

for i in range(n):
    tmp = list(map(int, input().split()))
    u.append(tmp[0])
    k.append(tmp[1])
    if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
        
check = [0]*n
intime = [0]*n
outtime = [0]*n
time = 1

def dfs(v, start):
    d = deque()
    d.append(start)
    global check
    global intime
    global outtime
    global time
    check[start-1] = 1
    intime[start-1] = time
    time += 1
    while len(d) > 0:
        visit = d[-1]
        tf = True
        for dtn in v[visit-1]:
            if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
        if tf:
            outtime[visit-1] = time
            d.pop()
            time += 1

dfs(v, 1)
for i in range(n):
    if check[i] == 0:
        dfs(v, i+1)
for i in range(n):
    print(i+1, intime[i], outtime[i]) from collections import deque n = int(input()) n int(input()) int input() input u = [] u [] k = [] k [] v = [] v [] for i in range(n):
    tmp = list(map(int, input().split()))
    u.append(tmp[0])
    k.append(tmp[1])
    if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
         i range(n) range n tmp = list(map(int, input().split())) tmp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split u.append(tmp[0]) u.append u append tmp[0] tmp 0 k.append(tmp[1]) k.append k append tmp[1] tmp 1 if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
         tmp[1] != 0 tmp[1] tmp 1 0 v.append(tmp[2:]) v.append v append tmp[2:] tmp 2: 2 v.append([]) v.append v append [] check = [0]*n check [0]*n [0] 0 n intime = [0]*n intime [0]*n [0] 0 n outtime = [0]*n outtime [0]*n [0] 0 n time = 1 time 1 def dfs(v, start):
    d = deque()
    d.append(start)
    global check
    global intime
    global outtime
    global time
    check[start-1] = 1
    intime[start-1] = time
    time += 1
    while len(d) > 0:
        visit = d[-1]
        tf = True
        for dtn in v[visit-1]:
            if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
        if tf:
            outtime[visit-1] = time
            d.pop()
            time += 1 dfs d = deque() d deque() deque d.append(start) d.append d append start global check global intime global outtime global time check[start-1] = 1 check[start-1] check start-1 start 1 1 intime[start-1] = time intime[start-1] intime start-1 start 1 time time += 1 time 1 while len(d) > 0:
        visit = d[-1]
        tf = True
        for dtn in v[visit-1]:
            if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
        if tf:
            outtime[visit-1] = time
            d.pop()
            time += 1 len(d) > 0 len(d) len d 0 visit = d[-1] visit d[-1] d -1 tf = True tf True for dtn in v[visit-1]:
            if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
         dtn v[visit-1] v visit-1 visit 1 if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
         check[dtn-1] == 0 check[dtn-1] check dtn-1 dtn 1 0 d.append(dtn) d.append d append dtn check[dtn-1] = 1 check[dtn-1] check dtn-1 dtn 1 1 intime[dtn-1] = time intime[dtn-1] intime dtn-1 dtn 1 time time += 1 time 1 tf = False tf False break if tf:
            outtime[visit-1] = time
            d.pop()
            time += 1 tf outtime[visit-1] = time outtime[visit-1] outtime visit-1 visit 1 time d.pop() d.pop d pop time += 1 time 1 v v start start dfs(v, 1) dfs v 1 for i in range(n):
    if check[i] == 0:
        dfs(v, i+1) i range(n) range n if check[i] == 0:
        dfs(v, i+1) check[i] == 0 check[i] check i 0 dfs(v, i+1) dfs v i+1 i 1 for i in range(n):
    print(i+1, intime[i], outtime[i]) i range(n) range n print(i+1, intime[i], outtime[i]) print i+1 i 1 intime[i] intime i outtime[i] outtime i time = 1 1 time n = int(input()) int(input()) n u = [] [] u check = [0]*n [0]*n check outtime = [0]*n [0]*n outtime k = [] [] k tmp = list(map(int, input().split())) list(map(int, input().split())) tmp intime = [0]*n [0]*n intime v = [] [] v def dfs(v, start):
    d = deque()
    d.append(start)
    global check
    global intime
    global outtime
    global time
    check[start-1] = 1
    intime[start-1] = time
    time += 1
    while len(d) > 0:
        visit = d[-1]
        tf = True
        for dtn in v[visit-1]:
            if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
        if tf:
            outtime[visit-1] = time
            d.pop()
            time += 1 def dfs(v, start):
    d = deque()
    d.append(start)
    global check
    global intime
    global outtime
    global time
    check[start-1] = 1
    intime[start-1] = time
    time += 1
    while len(d) > 0:
        visit = d[-1]
        tf = True
        for dtn in v[visit-1]:
            if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
        if tf:
            outtime[visit-1] = time
            d.pop()
            time += 1 dfs
from collections import deque
n = int(input())
u = []
k = []
v = []

for i in range(n):
    tmp = list(map(int, input().split()))
    u.append(tmp[0])
    k.append(tmp[1])
    if tmp[1] != 0:
        v.append(tmp[2:])
    else:
        v.append([])
        
check = [0]*n
intime = [0]*n
outtime = [0]*n
time = 1

def dfs(v, start):
    d = deque()
    d.append(start)
    global check
    global intime
    global outtime
    global time
    check[start-1] = 1
    intime[start-1] = time
    time += 1
    while len(d) > 0:
        visit = d[-1]
        tf = True
        for dtn in v[visit-1]:
            if check[dtn-1] == 0:
                d.append(dtn)
                check[dtn-1] = 1
                intime[dtn-1] = time
                time += 1
                tf = False
                break
        if tf:
            outtime[visit-1] = time
            d.pop()
            time += 1

dfs(v, 1)
for i in range(n):
    if check[i] == 0:
        dfs(v, i+1)
for i in range(n):
    print(i+1, intime[i], outtime[i])
