import abc


class AdjacentGraph:
    """Implementation adjacency-list Graph.
    Beware ids are between 1 and size.
    """
    def __init__(self, size):
        self.size = size
        self._nodes = [[0] * (size+1) for _ in range(size+1)]

    def set_adj_node(self, id_, adj_id):
        self._nodes[id_][adj_id] = 1

    def __iter__(self):
        self._id = 0
        return self

    def __next__(self):
        if self._id < self.size:
            self._id += 1
            return (self._id, self._nodes[self._id][1:])

        raise StopIteration()

    def dfs(self, handler=None):

        visited = []
        while len(visited) < self.size:
            for id_ in range(1, self.size+1):
                if id_ not in visited:
                    stack = [(id_, 0)]
                    break
            while len(stack) > 0:
                i, j = stack.pop()
                if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i)


class EventHandler(abc.ABC):
    @abc.abstractmethod
    def visit(self, i):
        pass

    @abc.abstractmethod
    def leave(self, i):
        pass


class Logger(EventHandler):
    def __init__(self, n):
        self.log = [(0, 0)] * n
        self.step = 0

    def visit(self, i):
        self.step += 1
        self.log[i-1] = (self.step, 0)

    def leave(self, i):
        self.step += 1
        (n, m) = self.log[i-1]
        self.log[i-1] = (n, self.step)

    def by_node(self):
        i = 1
        for discover, finish in self.log:
            yield (i, discover, finish)
            i += 1


def run():
    n = int(input())
    g = AdjacentGraph(n)
    log = Logger(n)

    for i in range(n):
        id_, c, *links = [int(x) for x in input().split()]
        for n in links:
            g.set_adj_node(id_, n)

    for i in g.dfs(log):
        pass

    for node in log.by_node():
        print(" ".join([str(i) for i in node]))


if __name__ == '__main__':
    run()
 import abc abc class AdjacentGraph:
    """Implementation adjacency-list Graph.
    Beware ids are between 1 and size.
    """
    def __init__(self, size):
        self.size = size
        self._nodes = [[0] * (size+1) for _ in range(size+1)]

    def set_adj_node(self, id_, adj_id):
        self._nodes[id_][adj_id] = 1

    def __iter__(self):
        self._id = 0
        return self

    def __next__(self):
        if self._id < self.size:
            self._id += 1
            return (self._id, self._nodes[self._id][1:])

        raise StopIteration()

    def dfs(self, handler=None):

        visited = []
        while len(visited) < self.size:
            for id_ in range(1, self.size+1):
                if id_ not in visited:
                    stack = [(id_, 0)]
                    break
            while len(stack) > 0:
                i, j = stack.pop()
                if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i) AdjacentGraph """Implementation adjacency-list Graph.
    Beware ids are between 1 and size.
    """ def __init__(self, size):
        self.size = size
        self._nodes = [[0] * (size+1) for _ in range(size+1)]

     __init__ self.size = size self.size self size size [0] * (size+1) for _ in range(size+1) for _ in range(size+1) _ range(size+1) range size+1 size 1 for _ in range(size+1) [0] * (size+1) [0] 0 size+1 size 1 self._nodes = [[0] * (size+1) for _ in range(size+1)] self._nodes self _nodes [[0] * (size+1) for _ in range(size+1)] self self size size def set_adj_node(self, id_, adj_id):
        self._nodes[id_][adj_id] = 1

     set_adj_node self._nodes[id_][adj_id] = 1 self._nodes[id_][adj_id] self._nodes[id_] self._nodes self _nodes id_ adj_id 1 self self id_ id_ adj_id adj_id def __iter__(self):
        self._id = 0
        return self

     __iter__ self._id = 0 self._id self _id 0 return self self self self def __next__(self):
        if self._id < self.size:
            self._id += 1
            return (self._id, self._nodes[self._id][1:])

        raise StopIteration()

     __next__ if self._id < self.size:
            self._id += 1
            return (self._id, self._nodes[self._id][1:])

         self._id < self.size self._id self _id self.size self size self._id += 1 self._id self _id 1 return (self._id, self._nodes[self._id][1:]) (self._id, self._nodes[self._id][1:]) self._id self _id self._nodes[self._id][1:] self._nodes[self._id] self._nodes self _nodes self._id self _id 1: 1 raise StopIteration() StopIteration() StopIteration self self def dfs(self, handler=None):

        visited = []
        while len(visited) < self.size:
            for id_ in range(1, self.size+1):
                if id_ not in visited:
                    stack = [(id_, 0)]
                    break
            while len(stack) > 0:
                i, j = stack.pop()
                if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i) dfs visited = [] visited [] while len(visited) < self.size:
            for id_ in range(1, self.size+1):
                if id_ not in visited:
                    stack = [(id_, 0)]
                    break
            while len(stack) > 0:
                i, j = stack.pop()
                if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i) len(visited) < self.size len(visited) len visited self.size self size for id_ in range(1, self.size+1):
                if id_ not in visited:
                    stack = [(id_, 0)]
                    break
             id_ range(1, self.size+1) range 1 self.size+1 self.size self size 1 if id_ not in visited:
                    stack = [(id_, 0)]
                    break
             id_ not in visited id_ visited stack = [(id_, 0)] stack [(id_, 0)] (id_, 0) id_ 0 break while len(stack) > 0:
                i, j = stack.pop()
                if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i) len(stack) > 0 len(stack) len stack 0 i, j = stack.pop() i stack.pop() stack.pop stack pop j if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                 j == 0 j 0 if handler:
                        handler.visit(i)
                     handler handler.visit(i) handler.visit handler visit i visited.append(i) visited.append visited append i yield i i try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i) except ValueError:
                    if handler:
                        handler.leave(i) if handler:
                        handler.leave(i) handler handler.leave(i) handler.leave handler leave i j = self._nodes[i].index(1, j+1) j self._nodes[i].index(1, j+1) self._nodes[i].index self._nodes[i] self._nodes self _nodes i index 1 j+1 j 1 stack.append((i, j)) stack.append stack append (i, j) i j if j not in visited:
                        stack.append((j, 0))
                 j not in visited j visited stack.append((j, 0)) stack.append stack append (j, 0) j 0 self self handler=None handler None class EventHandler(abc.ABC):
    @abc.abstractmethod
    def visit(self, i):
        pass

    @abc.abstractmethod
    def leave(self, i):
        pass EventHandler def visit(self, i):
        pass

     visit self self i i @abc.abstractmethod
     visit abc.abstractmethod visit def leave(self, i):
        pass leave self self i i @abc.abstractmethod
     leave abc.abstractmethod leave abc.ABC abc ABC class Logger(EventHandler):
    def __init__(self, n):
        self.log = [(0, 0)] * n
        self.step = 0

    def visit(self, i):
        self.step += 1
        self.log[i-1] = (self.step, 0)

    def leave(self, i):
        self.step += 1
        (n, m) = self.log[i-1]
        self.log[i-1] = (n, self.step)

    def by_node(self):
        i = 1
        for discover, finish in self.log:
            yield (i, discover, finish)
            i += 1 Logger def __init__(self, n):
        self.log = [(0, 0)] * n
        self.step = 0

     __init__ self.log = [(0, 0)] * n self.log self log [(0, 0)] * n [(0, 0)] (0, 0) 0 0 n self.step = 0 self.step self step 0 self self n n def visit(self, i):
        self.step += 1
        self.log[i-1] = (self.step, 0)

     visit self.step += 1 self.step self step 1 self.log[i-1] = (self.step, 0) self.log[i-1] self.log self log i-1 i 1 (self.step, 0) self.step self step 0 self self i i def leave(self, i):
        self.step += 1
        (n, m) = self.log[i-1]
        self.log[i-1] = (n, self.step)

     leave self.step += 1 self.step self step 1 (n, m) = self.log[i-1] (n, m) n m self.log[i-1] self.log self log i-1 i 1 self.log[i-1] = (n, self.step) self.log[i-1] self.log self log i-1 i 1 (n, self.step) n self.step self step self self i i def by_node(self):
        i = 1
        for discover, finish in self.log:
            yield (i, discover, finish)
            i += 1 by_node i = 1 i 1 for discover, finish in self.log:
            yield (i, discover, finish)
            i += 1 discover finish self.log self log yield (i, discover, finish) (i, discover, finish) i discover finish i += 1 i 1 self self EventHandler def run():
    n = int(input())
    g = AdjacentGraph(n)
    log = Logger(n)

    for i in range(n):
        id_, c, *links = [int(x) for x in input().split()]
        for n in links:
            g.set_adj_node(id_, n)

    for i in g.dfs(log):
        pass

    for node in log.by_node():
        print(" ".join([str(i) for i in node])) run n = int(input()) n int(input()) int input() input g = AdjacentGraph(n) g AdjacentGraph(n) AdjacentGraph n log = Logger(n) log Logger(n) Logger n for i in range(n):
        id_, c, *links = [int(x) for x in input().split()]
        for n in links:
            g.set_adj_node(id_, n)

     i range(n) range n int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x id_, c, *links = [int(x) for x in input().split()] id_ [int(x) for x in input().split()] c *links links for n in links:
            g.set_adj_node(id_, n)

     n links g.set_adj_node(id_, n) g.set_adj_node g set_adj_node id_ n for i in g.dfs(log):
        pass

     i g.dfs(log) g.dfs g dfs log for node in log.by_node():
        print(" ".join([str(i) for i in node])) node log.by_node() log.by_node log by_node str(i) for i in node for i in node i node for i in node str(i) str i print(" ".join([str(i) for i in node])) print " ".join([str(i) for i in node]) " ".join " " join [str(i) for i in node] if __name__ == '__main__':
    run() __name__ == '__main__' __name__ '__main__' run() run def run():
    n = int(input())
    g = AdjacentGraph(n)
    log = Logger(n)

    for i in range(n):
        id_, c, *links = [int(x) for x in input().split()]
        for n in links:
            g.set_adj_node(id_, n)

    for i in g.dfs(log):
        pass

    for node in log.by_node():
        print(" ".join([str(i) for i in node])) def run():
    n = int(input())
    g = AdjacentGraph(n)
    log = Logger(n)

    for i in range(n):
        id_, c, *links = [int(x) for x in input().split()]
        for n in links:
            g.set_adj_node(id_, n)

    for i in g.dfs(log):
        pass

    for node in log.by_node():
        print(" ".join([str(i) for i in node])) run class EventHandler(abc.ABC):
    @abc.abstractmethod
    def visit(self, i):
        pass

    @abc.abstractmethod
    def leave(self, i):
        pass class EventHandler(abc.ABC):
    @abc.abstractmethod
    def visit(self, i):
        pass

    @abc.abstractmethod
    def leave(self, i):
        pass EventHandler class AdjacentGraph:
    """Implementation adjacency-list Graph.
    Beware ids are between 1 and size.
    """
    def __init__(self, size):
        self.size = size
        self._nodes = [[0] * (size+1) for _ in range(size+1)]

    def set_adj_node(self, id_, adj_id):
        self._nodes[id_][adj_id] = 1

    def __iter__(self):
        self._id = 0
        return self

    def __next__(self):
        if self._id < self.size:
            self._id += 1
            return (self._id, self._nodes[self._id][1:])

        raise StopIteration()

    def dfs(self, handler=None):

        visited = []
        while len(visited) < self.size:
            for id_ in range(1, self.size+1):
                if id_ not in visited:
                    stack = [(id_, 0)]
                    break
            while len(stack) > 0:
                i, j = stack.pop()
                if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i) class AdjacentGraph:
    """Implementation adjacency-list Graph.
    Beware ids are between 1 and size.
    """
    def __init__(self, size):
        self.size = size
        self._nodes = [[0] * (size+1) for _ in range(size+1)]

    def set_adj_node(self, id_, adj_id):
        self._nodes[id_][adj_id] = 1

    def __iter__(self):
        self._id = 0
        return self

    def __next__(self):
        if self._id < self.size:
            self._id += 1
            return (self._id, self._nodes[self._id][1:])

        raise StopIteration()

    def dfs(self, handler=None):

        visited = []
        while len(visited) < self.size:
            for id_ in range(1, self.size+1):
                if id_ not in visited:
                    stack = [(id_, 0)]
                    break
            while len(stack) > 0:
                i, j = stack.pop()
                if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i) AdjacentGraph class Logger(EventHandler):
    def __init__(self, n):
        self.log = [(0, 0)] * n
        self.step = 0

    def visit(self, i):
        self.step += 1
        self.log[i-1] = (self.step, 0)

    def leave(self, i):
        self.step += 1
        (n, m) = self.log[i-1]
        self.log[i-1] = (n, self.step)

    def by_node(self):
        i = 1
        for discover, finish in self.log:
            yield (i, discover, finish)
            i += 1 class Logger(EventHandler):
    def __init__(self, n):
        self.log = [(0, 0)] * n
        self.step = 0

    def visit(self, i):
        self.step += 1
        self.log[i-1] = (self.step, 0)

    def leave(self, i):
        self.step += 1
        (n, m) = self.log[i-1]
        self.log[i-1] = (n, self.step)

    def by_node(self):
        i = 1
        for discover, finish in self.log:
            yield (i, discover, finish)
            i += 1 Logger
import abc


class AdjacentGraph:
    """Implementation adjacency-list Graph.
    Beware ids are between 1 and size.
    """
    def __init__(self, size):
        self.size = size
        self._nodes = [[0] * (size+1) for _ in range(size+1)]

    def set_adj_node(self, id_, adj_id):
        self._nodes[id_][adj_id] = 1

    def __iter__(self):
        self._id = 0
        return self

    def __next__(self):
        if self._id < self.size:
            self._id += 1
            return (self._id, self._nodes[self._id][1:])

        raise StopIteration()

    def dfs(self, handler=None):

        visited = []
        while len(visited) < self.size:
            for id_ in range(1, self.size+1):
                if id_ not in visited:
                    stack = [(id_, 0)]
                    break
            while len(stack) > 0:
                i, j = stack.pop()
                if j == 0:
                    if handler:
                        handler.visit(i)
                    visited.append(i)
                    yield i
                try:
                    j = self._nodes[i].index(1, j+1)
                    stack.append((i, j))
                    if j not in visited:
                        stack.append((j, 0))
                except ValueError:
                    if handler:
                        handler.leave(i)


class EventHandler(abc.ABC):
    @abc.abstractmethod
    def visit(self, i):
        pass

    @abc.abstractmethod
    def leave(self, i):
        pass


class Logger(EventHandler):
    def __init__(self, n):
        self.log = [(0, 0)] * n
        self.step = 0

    def visit(self, i):
        self.step += 1
        self.log[i-1] = (self.step, 0)

    def leave(self, i):
        self.step += 1
        (n, m) = self.log[i-1]
        self.log[i-1] = (n, self.step)

    def by_node(self):
        i = 1
        for discover, finish in self.log:
            yield (i, discover, finish)
            i += 1


def run():
    n = int(input())
    g = AdjacentGraph(n)
    log = Logger(n)

    for i in range(n):
        id_, c, *links = [int(x) for x in input().split()]
        for n in links:
            g.set_adj_node(id_, n)

    for i in g.dfs(log):
        pass

    for node in log.by_node():
        print(" ".join([str(i) for i in node]))


if __name__ == '__main__':
    run()

