class Node:

    def __init__(self):
        self.distime = 0
        self.fintime = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

    @property
    def status(self):
        return bool(self.distime) + bool(self.fintime)

class Stack:
    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.top = 0

    def is_emtpy(self):
        return self.top == 0

    def is_full(self):
        return self.top == self.size

    def front(self):
        return self.N[self.top - 1]

    def push(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.top] = x
        self.top += 1

    def pop(self):
        if self.is_emtpy():
            raise IndexError
        self.top -= 1
        return self.N[self.top]


def visit(node: Node):
    global t
    stack.push(node)
    t += 1
    node.distime = t
    while not stack.is_emtpy():
        u: Node = stack.front()
        try:
            v = next(i for i in u.v if i.status == 0)
        except StopIteration:
            stack.pop()
            t += 1
            u.fintime = t
        else:
            if v.status == 0:
                stack.push(v)
                t += 1
                v.distime = t


n = int(input())
stack = Stack(n)
nodes = [Node() for _ in range(n)]
[node.input_data(nodes) for node in nodes]
t = 0
for node in nodes:
    if node.status == 0:
        visit(node)

nodes.sort(key=lambda i: i.number)
for node in nodes:
    print(f"{node.number} {node.distime} {node.fintime}") class Node:

    def __init__(self):
        self.distime = 0
        self.fintime = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

    @property
    def status(self):
        return bool(self.distime) + bool(self.fintime) Node def __init__(self):
        self.distime = 0
        self.fintime = 0

     __init__ self.distime = 0 self.distime self distime 0 self.fintime = 0 self.fintime self fintime 0 self self def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

     input_data data = input().split() data input().split() ().split () input split self.number = int(data[0]) self.number self number int(data[0]) int data[0] data 0 nodes[int(x) - 1] for x in data[2:] for x in data[2:] x data[2:] data 2: 2 for x in data[2:] nodes[int(x) - 1] nodes int(x) - 1 int(x) int x 1 self.v = [nodes[int(x) - 1] for x in data[2:]] self.v self v [nodes[int(x) - 1] for x in data[2:]] self self nodes nodes def status(self):
        return bool(self.distime) + bool(self.fintime) status return bool(self.distime) + bool(self.fintime) bool(self.distime) + bool(self.fintime) bool(self.distime) bool self.distime self distime bool(self.fintime) bool self.fintime self fintime self self @property
     status property status class Stack:
    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.top = 0

    def is_emtpy(self):
        return self.top == 0

    def is_full(self):
        return self.top == self.size

    def front(self):
        return self.N[self.top - 1]

    def push(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.top] = x
        self.top += 1

    def pop(self):
        if self.is_emtpy():
            raise IndexError
        self.top -= 1
        return self.N[self.top] Stack def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.top = 0

     __init__ self.size = size self.size self size size None for _ in range(size) for _ in range(size) _ range(size) range size for _ in range(size) None self.N = [None for _ in range(size)] self.N self N [None for _ in range(size)] self.top = 0 self.top self top 0 self self size size def is_emtpy(self):
        return self.top == 0

     is_emtpy return self.top == 0 self.top == 0 self.top self top 0 self self def is_full(self):
        return self.top == self.size

     is_full return self.top == self.size self.top == self.size self.top self top self.size self size self self def front(self):
        return self.N[self.top - 1]

     front return self.N[self.top - 1] self.N[self.top - 1] self.N self N self.top - 1 self.top self top 1 self self def push(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.top] = x
        self.top += 1

     push if self.is_full():
            raise OverflowError
         self.is_full() self.is_full self is_full raise OverflowError OverflowError self.N[self.top] = x self.N[self.top] self.N self N self.top self top x self.top += 1 self.top self top 1 self self x x def pop(self):
        if self.is_emtpy():
            raise IndexError
        self.top -= 1
        return self.N[self.top] pop if self.is_emtpy():
            raise IndexError
         self.is_emtpy() self.is_emtpy self is_emtpy raise IndexError IndexError self.top -= 1 self.top self top 1 return self.N[self.top] self.N[self.top] self.N self N self.top self top self self def visit(node: Node):
    global t
    stack.push(node)
    t += 1
    node.distime = t
    while not stack.is_emtpy():
        u: Node = stack.front()
        try:
            v = next(i for i in u.v if i.status == 0)
        except StopIteration:
            stack.pop()
            t += 1
            u.fintime = t
        else:
            if v.status == 0:
                stack.push(v)
                t += 1
                v.distime = t visit global t stack.push(node) stack.push stack push node t += 1 t 1 node.distime = t node.distime node distime t while not stack.is_emtpy():
        u: Node = stack.front()
        try:
            v = next(i for i in u.v if i.status == 0)
        except StopIteration:
            stack.pop()
            t += 1
            u.fintime = t
        else:
            if v.status == 0:
                stack.push(v)
                t += 1
                v.distime = t not stack.is_emtpy() stack.is_emtpy() stack.is_emtpy stack is_emtpy u: Node = stack.front() u stack.front() stack.front stack front try:
            v = next(i for i in u.v if i.status == 0)
        except StopIteration:
            stack.pop()
            t += 1
            u.fintime = t
        else:
            if v.status == 0:
                stack.push(v)
                t += 1
                v.distime = t except StopIteration:
            stack.pop()
            t += 1
            u.fintime = t
         stack.pop() stack.pop stack pop t += 1 t 1 u.fintime = t u.fintime u fintime t v = next(i for i in u.v if i.status == 0) v next(i for i in u.v if i.status == 0) next i if v.status == 0:
                stack.push(v)
                t += 1
                v.distime = t v.status == 0 v.status v status 0 stack.push(v) stack.push stack push v t += 1 t 1 v.distime = t v.distime v distime t node: Node node n = int(input()) n int(input()) int input() input stack = Stack(n) stack Stack(n) Stack n Node() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) Node() Node nodes = [Node() for _ in range(n)] nodes [Node() for _ in range(n)] node.input_data(nodes) for node in nodes for node in nodes node nodes for node in nodes node.input_data(nodes) node.input_data node input_data nodes [node.input_data(nodes) for node in nodes] t = 0 t 0 for node in nodes:
    if node.status == 0:
        visit(node) node nodes if node.status == 0:
        visit(node) node.status == 0 node.status node status 0 visit(node) visit node nodes.sort(key=lambda i: i.number) nodes.sort nodes sort key=lambda i: i.number key lambda i: i.number i.number i number i for node in nodes:
    print(f"{node.number} {node.distime} {node.fintime}") node nodes print(f"{node.number} {node.distime} {node.fintime}") print f"{node.number} {node.distime} {node.fintime}" nodes = [Node() for _ in range(n)] [Node() for _ in range(n)] nodes t = 0 0 t stack = Stack(n) Stack(n) stack n = int(input()) int(input()) n class Stack:
    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.top = 0

    def is_emtpy(self):
        return self.top == 0

    def is_full(self):
        return self.top == self.size

    def front(self):
        return self.N[self.top - 1]

    def push(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.top] = x
        self.top += 1

    def pop(self):
        if self.is_emtpy():
            raise IndexError
        self.top -= 1
        return self.N[self.top] class Stack:
    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.top = 0

    def is_emtpy(self):
        return self.top == 0

    def is_full(self):
        return self.top == self.size

    def front(self):
        return self.N[self.top - 1]

    def push(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.top] = x
        self.top += 1

    def pop(self):
        if self.is_emtpy():
            raise IndexError
        self.top -= 1
        return self.N[self.top] Stack def visit(node: Node):
    global t
    stack.push(node)
    t += 1
    node.distime = t
    while not stack.is_emtpy():
        u: Node = stack.front()
        try:
            v = next(i for i in u.v if i.status == 0)
        except StopIteration:
            stack.pop()
            t += 1
            u.fintime = t
        else:
            if v.status == 0:
                stack.push(v)
                t += 1
                v.distime = t def visit(node: Node):
    global t
    stack.push(node)
    t += 1
    node.distime = t
    while not stack.is_emtpy():
        u: Node = stack.front()
        try:
            v = next(i for i in u.v if i.status == 0)
        except StopIteration:
            stack.pop()
            t += 1
            u.fintime = t
        else:
            if v.status == 0:
                stack.push(v)
                t += 1
                v.distime = t visit class Node:

    def __init__(self):
        self.distime = 0
        self.fintime = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

    @property
    def status(self):
        return bool(self.distime) + bool(self.fintime) class Node:

    def __init__(self):
        self.distime = 0
        self.fintime = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

    @property
    def status(self):
        return bool(self.distime) + bool(self.fintime) Node
class Node:

    def __init__(self):
        self.distime = 0
        self.fintime = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

    @property
    def status(self):
        return bool(self.distime) + bool(self.fintime)

class Stack:
    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.top = 0

    def is_emtpy(self):
        return self.top == 0

    def is_full(self):
        return self.top == self.size

    def front(self):
        return self.N[self.top - 1]

    def push(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.top] = x
        self.top += 1

    def pop(self):
        if self.is_emtpy():
            raise IndexError
        self.top -= 1
        return self.N[self.top]


def visit(node: Node):
    global t
    stack.push(node)
    t += 1
    node.distime = t
    while not stack.is_emtpy():
        u: Node = stack.front()
        try:
            v = next(i for i in u.v if i.status == 0)
        except StopIteration:
            stack.pop()
            t += 1
            u.fintime = t
        else:
            if v.status == 0:
                stack.push(v)
                t += 1
                v.distime = t


n = int(input())
stack = Stack(n)
nodes = [Node() for _ in range(n)]
[node.input_data(nodes) for node in nodes]
t = 0
for node in nodes:
    if node.status == 0:
        visit(node)

nodes.sort(key=lambda i: i.number)
for node in nodes:
    print(f"{node.number} {node.distime} {node.fintime}")
