# coding: utf-8
import itertools
import math
import bisect

class Node:
    def __init__(self, id, next, rest):
        self.id = id
        self.before = None
        self.next = next
        self.rest = rest
        self.d = None
        self.f = None

    def update_next(self):
        self.next = first(self.rest)
        self.rest = rest(self.rest)


N = int(input())
graph = []
for _ in range(N):
    l = list(map(int, input().split(" ")))
    id = l[0]
    if l[1] != 0:
        next = l[2]
    else:
        next = None
    if l[1] >= 2:
        rest = l[3:]
    else:
        rest = []
    graph.append(Node(l[0], next, rest))

def first(l):
    if l==None:
        return None
    if len(l) == 0:
        return None
    else:
        return l[0]

def rest(l):
    if l == None:
        return None
    if len(l) >= 2:
        return l[1:]
    else:
        return None

def dfs(graph):
    stack = [graph[0]]
    stack[-1].d = 1
    tansakuzumi = [1]
    mitansaku = list(range(1, len(graph)+1))
    res_graph = []
    t = 2
    while len(stack) != 0:
        tansakuzumi.append(stack[-1].id)
        if stack[-1].id in mitansaku:
            mitansaku.remove(stack[-1].id)
        if stack[-1].next == None:
            stack[-1].f = t
            res_graph.append(stack.pop())
            t += 1
        elif stack[-1].next in tansakuzumi:
            stack[-1].update_next()
        else:
            stack.append(graph[stack[-1].next-1])
            stack[-2].update_next()
            stack[-1].d = t
            t += 1
        if len(stack) == 0 and len(mitansaku) >= 1:
            stack.append(graph[mitansaku[0]-1])
            stack[-1].d = t
            t += 1
    return res_graph




res_graph = dfs(graph)

res_graph.sort(key=lambda x: x.id)

for n in res_graph:
    print("{} {} {}".format(n.id, n.d, n.f))
 import itertools itertools import math math import bisect bisect class Node:
    def __init__(self, id, next, rest):
        self.id = id
        self.before = None
        self.next = next
        self.rest = rest
        self.d = None
        self.f = None

    def update_next(self):
        self.next = first(self.rest)
        self.rest = rest(self.rest) Node def __init__(self, id, next, rest):
        self.id = id
        self.before = None
        self.next = next
        self.rest = rest
        self.d = None
        self.f = None

     __init__ self.id = id self.id self id id self.before = None self.before self before None self.next = next self.next self next next self.rest = rest self.rest self rest rest self.d = None self.d self d None self.f = None self.f self f None self self id id next next rest rest def update_next(self):
        self.next = first(self.rest)
        self.rest = rest(self.rest) update_next self.next = first(self.rest) self.next self next first(self.rest) first self.rest self rest self.rest = rest(self.rest) self.rest self rest rest(self.rest) rest self.rest self rest self self N = int(input()) N int(input()) int input() input graph = [] graph [] for _ in range(N):
    l = list(map(int, input().split(" ")))
    id = l[0]
    if l[1] != 0:
        next = l[2]
    else:
        next = None
    if l[1] >= 2:
        rest = l[3:]
    else:
        rest = []
    graph.append(Node(l[0], next, rest)) _ range(N) range N l = list(map(int, input().split(" "))) l list(map(int, input().split(" "))) list map(int, input().split(" ")) map int input().split(" ") ().split () input split " " id = l[0] id l[0] l 0 if l[1] != 0:
        next = l[2]
    else:
        next = None
     l[1] != 0 l[1] l 1 0 next = l[2] next l[2] l 2 next = None next None if l[1] >= 2:
        rest = l[3:]
    else:
        rest = []
     l[1] >= 2 l[1] l 1 2 rest = l[3:] rest l[3:] l 3: 3 rest = [] rest [] graph.append(Node(l[0], next, rest)) graph.append graph append Node(l[0], next, rest) Node l[0] l 0 next rest def first(l):
    if l==None:
        return None
    if len(l) == 0:
        return None
    else:
        return l[0] first if l==None:
        return None
     l==None l None return None None if len(l) == 0:
        return None
    else:
        return l[0] len(l) == 0 len(l) len l 0 return None None return l[0] l[0] l 0 l l def rest(l):
    if l == None:
        return None
    if len(l) >= 2:
        return l[1:]
    else:
        return None rest if l == None:
        return None
     l == None l None return None None if len(l) >= 2:
        return l[1:]
    else:
        return None len(l) >= 2 len(l) len l 2 return l[1:] l[1:] l 1: 1 return None None l l def dfs(graph):
    stack = [graph[0]]
    stack[-1].d = 1
    tansakuzumi = [1]
    mitansaku = list(range(1, len(graph)+1))
    res_graph = []
    t = 2
    while len(stack) != 0:
        tansakuzumi.append(stack[-1].id)
        if stack[-1].id in mitansaku:
            mitansaku.remove(stack[-1].id)
        if stack[-1].next == None:
            stack[-1].f = t
            res_graph.append(stack.pop())
            t += 1
        elif stack[-1].next in tansakuzumi:
            stack[-1].update_next()
        else:
            stack.append(graph[stack[-1].next-1])
            stack[-2].update_next()
            stack[-1].d = t
            t += 1
        if len(stack) == 0 and len(mitansaku) >= 1:
            stack.append(graph[mitansaku[0]-1])
            stack[-1].d = t
            t += 1
    return res_graph dfs stack = [graph[0]] stack [graph[0]] graph[0] graph 0 stack[-1].d = 1 stack[-1].d [-1] stack -1 d 1 tansakuzumi = [1] tansakuzumi [1] 1 mitansaku = list(range(1, len(graph)+1)) mitansaku list(range(1, len(graph)+1)) list range(1, len(graph)+1) range 1 len(graph)+1 len(graph) len graph 1 res_graph = [] res_graph [] t = 2 t 2 while len(stack) != 0:
        tansakuzumi.append(stack[-1].id)
        if stack[-1].id in mitansaku:
            mitansaku.remove(stack[-1].id)
        if stack[-1].next == None:
            stack[-1].f = t
            res_graph.append(stack.pop())
            t += 1
        elif stack[-1].next in tansakuzumi:
            stack[-1].update_next()
        else:
            stack.append(graph[stack[-1].next-1])
            stack[-2].update_next()
            stack[-1].d = t
            t += 1
        if len(stack) == 0 and len(mitansaku) >= 1:
            stack.append(graph[mitansaku[0]-1])
            stack[-1].d = t
            t += 1
     len(stack) != 0 len(stack) len stack 0 tansakuzumi.append(stack[-1].id) tansakuzumi.append tansakuzumi append stack[-1].id [-1] stack -1 id if stack[-1].id in mitansaku:
            mitansaku.remove(stack[-1].id)
         stack[-1].id in mitansaku stack[-1].id [-1] stack -1 id mitansaku mitansaku.remove(stack[-1].id) mitansaku.remove mitansaku remove stack[-1].id [-1] stack -1 id if stack[-1].next == None:
            stack[-1].f = t
            res_graph.append(stack.pop())
            t += 1
        elif stack[-1].next in tansakuzumi:
            stack[-1].update_next()
        else:
            stack.append(graph[stack[-1].next-1])
            stack[-2].update_next()
            stack[-1].d = t
            t += 1
         stack[-1].next == None stack[-1].next [-1] stack -1 next None stack[-1].f = t stack[-1].f [-1] stack -1 f t res_graph.append(stack.pop()) res_graph.append res_graph append stack.pop() stack.pop stack pop t += 1 t 1 elif stack[-1].next in tansakuzumi:
            stack[-1].update_next()
         stack[-1].next in tansakuzumi stack[-1].next [-1] stack -1 next tansakuzumi stack[-1].update_next() [-1].update_next [-1] stack -1 update_next stack.append(graph[stack[-1].next-1]) stack.append stack append graph[stack[-1].next-1] graph stack[-1].next-1 stack[-1].next [-1] stack -1 next 1 stack[-2].update_next() [-2].update_next [-2] stack -2 update_next stack[-1].d = t stack[-1].d [-1] stack -1 d t t += 1 t 1 if len(stack) == 0 and len(mitansaku) >= 1:
            stack.append(graph[mitansaku[0]-1])
            stack[-1].d = t
            t += 1
     len(stack) == 0 and len(mitansaku) >= 1 len(stack) == 0 len(stack) len stack 0 len(mitansaku) >= 1 len(mitansaku) len mitansaku 1 stack.append(graph[mitansaku[0]-1]) stack.append stack append graph[mitansaku[0]-1] graph mitansaku[0]-1 mitansaku[0] mitansaku 0 1 stack[-1].d = t stack[-1].d [-1] stack -1 d t t += 1 t 1 return res_graph res_graph graph graph res_graph = dfs(graph) res_graph dfs(graph) dfs graph res_graph.sort(key=lambda x: x.id) res_graph.sort res_graph sort key=lambda x: x.id key lambda x: x.id x.id x id x for n in res_graph:
    print("{} {} {}".format(n.id, n.d, n.f)) n res_graph print("{} {} {}".format(n.id, n.d, n.f)) print "{} {} {}".format(n.id, n.d, n.f) "{} {} {}".format "{} {} {}" format n.id n id n.d n d n.f n f id = l[0] l[0] id rest = [] [] rest def first(l):
    if l==None:
        return None
    if len(l) == 0:
        return None
    else:
        return l[0] def first(l):
    if l==None:
        return None
    if len(l) == 0:
        return None
    else:
        return l[0] first def rest(l):
    if l == None:
        return None
    if len(l) >= 2:
        return l[1:]
    else:
        return None def rest(l):
    if l == None:
        return None
    if len(l) >= 2:
        return l[1:]
    else:
        return None rest def dfs(graph):
    stack = [graph[0]]
    stack[-1].d = 1
    tansakuzumi = [1]
    mitansaku = list(range(1, len(graph)+1))
    res_graph = []
    t = 2
    while len(stack) != 0:
        tansakuzumi.append(stack[-1].id)
        if stack[-1].id in mitansaku:
            mitansaku.remove(stack[-1].id)
        if stack[-1].next == None:
            stack[-1].f = t
            res_graph.append(stack.pop())
            t += 1
        elif stack[-1].next in tansakuzumi:
            stack[-1].update_next()
        else:
            stack.append(graph[stack[-1].next-1])
            stack[-2].update_next()
            stack[-1].d = t
            t += 1
        if len(stack) == 0 and len(mitansaku) >= 1:
            stack.append(graph[mitansaku[0]-1])
            stack[-1].d = t
            t += 1
    return res_graph def dfs(graph):
    stack = [graph[0]]
    stack[-1].d = 1
    tansakuzumi = [1]
    mitansaku = list(range(1, len(graph)+1))
    res_graph = []
    t = 2
    while len(stack) != 0:
        tansakuzumi.append(stack[-1].id)
        if stack[-1].id in mitansaku:
            mitansaku.remove(stack[-1].id)
        if stack[-1].next == None:
            stack[-1].f = t
            res_graph.append(stack.pop())
            t += 1
        elif stack[-1].next in tansakuzumi:
            stack[-1].update_next()
        else:
            stack.append(graph[stack[-1].next-1])
            stack[-2].update_next()
            stack[-1].d = t
            t += 1
        if len(stack) == 0 and len(mitansaku) >= 1:
            stack.append(graph[mitansaku[0]-1])
            stack[-1].d = t
            t += 1
    return res_graph dfs N = int(input()) int(input()) N next = None None next graph = [] [] graph l = list(map(int, input().split(" "))) list(map(int, input().split(" "))) l next = l[2] l[2] next rest = l[3:] l[3:] rest res_graph = dfs(graph) dfs(graph) res_graph class Node:
    def __init__(self, id, next, rest):
        self.id = id
        self.before = None
        self.next = next
        self.rest = rest
        self.d = None
        self.f = None

    def update_next(self):
        self.next = first(self.rest)
        self.rest = rest(self.rest) class Node:
    def __init__(self, id, next, rest):
        self.id = id
        self.before = None
        self.next = next
        self.rest = rest
        self.d = None
        self.f = None

    def update_next(self):
        self.next = first(self.rest)
        self.rest = rest(self.rest) Node
# coding: utf-8
import itertools
import math
import bisect

class Node:
    def __init__(self, id, next, rest):
        self.id = id
        self.before = None
        self.next = next
        self.rest = rest
        self.d = None
        self.f = None

    def update_next(self):
        self.next = first(self.rest)
        self.rest = rest(self.rest)


N = int(input())
graph = []
for _ in range(N):
    l = list(map(int, input().split(" ")))
    id = l[0]
    if l[1] != 0:
        next = l[2]
    else:
        next = None
    if l[1] >= 2:
        rest = l[3:]
    else:
        rest = []
    graph.append(Node(l[0], next, rest))

def first(l):
    if l==None:
        return None
    if len(l) == 0:
        return None
    else:
        return l[0]

def rest(l):
    if l == None:
        return None
    if len(l) >= 2:
        return l[1:]
    else:
        return None

def dfs(graph):
    stack = [graph[0]]
    stack[-1].d = 1
    tansakuzumi = [1]
    mitansaku = list(range(1, len(graph)+1))
    res_graph = []
    t = 2
    while len(stack) != 0:
        tansakuzumi.append(stack[-1].id)
        if stack[-1].id in mitansaku:
            mitansaku.remove(stack[-1].id)
        if stack[-1].next == None:
            stack[-1].f = t
            res_graph.append(stack.pop())
            t += 1
        elif stack[-1].next in tansakuzumi:
            stack[-1].update_next()
        else:
            stack.append(graph[stack[-1].next-1])
            stack[-2].update_next()
            stack[-1].d = t
            t += 1
        if len(stack) == 0 and len(mitansaku) >= 1:
            stack.append(graph[mitansaku[0]-1])
            stack[-1].d = t
            t += 1
    return res_graph




res_graph = dfs(graph)

res_graph.sort(key=lambda x: x.id)

for n in res_graph:
    print("{} {} {}".format(n.id, n.d, n.f))

