#!/usr/bin/env python3
from enum import Enum


class DFS():
    class Color(Enum):
        WHITE = 0
        GRAY = 1
        BLACK = 2

    def __init__(self, M, n):
        self.M = M
        self.n = n
        self.color = [self.Color.WHITE for _ in range(self.n)]
        self.d, self.f = [-1] * self.n, [-1] * self.n
        self.tt = 0
        self.nt = [0] * self.n

    def next(self, u: int):
        while self.nt[u] < self.n:
            v = self.nt[u]
            self.nt[u] = v + 1
            if self.M[u][v] != 0:
                return v
        return -1

    def dfs_visit(self, r: int):
        S = []                  # list をスタックとして使う
        S.append(r)
        self.color[r] = self.Color.GRAY
        self.tt += 1
        self.d[r] = self.tt
        while len(S) > 0:
            # スタックの一番上（= listの末尾）の値を取得 ※ pop()するとlistからなくなるので[-1]でアクセス
            u = S[-1]
            v = self.next(u)
            if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

    def dfs(self):
        for u in range(self.n):
            if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

        for i in range(self.n):
            print(f'{i+1} {self.d[i]} {self.f[i]}')


def main():
    n = int(input())
    M = [[0] * n for _ in range(n)]

    for i in range(n):
        line = input()
        inputs = [int(x) for x in line.split(' ')]
        u = inputs.pop(0)
        u -= 1                  # 0 オリジンへ変換
        _ = inputs.pop(0)       # k （使わない...）
        for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

    DFS(M, n).dfs()


if __name__ == '__main__':
    main()
 from enum import Enum class DFS():
    class Color(Enum):
        WHITE = 0
        GRAY = 1
        BLACK = 2

    def __init__(self, M, n):
        self.M = M
        self.n = n
        self.color = [self.Color.WHITE for _ in range(self.n)]
        self.d, self.f = [-1] * self.n, [-1] * self.n
        self.tt = 0
        self.nt = [0] * self.n

    def next(self, u: int):
        while self.nt[u] < self.n:
            v = self.nt[u]
            self.nt[u] = v + 1
            if self.M[u][v] != 0:
                return v
        return -1

    def dfs_visit(self, r: int):
        S = []                  # list をスタックとして使う
        S.append(r)
        self.color[r] = self.Color.GRAY
        self.tt += 1
        self.d[r] = self.tt
        while len(S) > 0:
            # スタックの一番上（= listの末尾）の値を取得 ※ pop()するとlistからなくなるので[-1]でアクセス
            u = S[-1]
            v = self.next(u)
            if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

    def dfs(self):
        for u in range(self.n):
            if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

        for i in range(self.n):
            print(f'{i+1} {self.d[i]} {self.f[i]}') DFS class Color(Enum):
        WHITE = 0
        GRAY = 1
        BLACK = 2

     Color WHITE = 0 WHITE 0 GRAY = 1 GRAY 1 BLACK = 2 BLACK 2 Enum def __init__(self, M, n):
        self.M = M
        self.n = n
        self.color = [self.Color.WHITE for _ in range(self.n)]
        self.d, self.f = [-1] * self.n, [-1] * self.n
        self.tt = 0
        self.nt = [0] * self.n

     __init__ self.M = M self.M self M M self.n = n self.n self n n self.Color.WHITE for _ in range(self.n) for _ in range(self.n) _ range(self.n) range self.n self n for _ in range(self.n) self.Color.WHITE self.Color self Color WHITE self.color = [self.Color.WHITE for _ in range(self.n)] self.color self color [self.Color.WHITE for _ in range(self.n)] self.d, self.f = [-1] * self.n, [-1] * self.n self.d self d [-1] * self.n [-1] -1 self.n self n self.f self f [-1] * self.n [-1] -1 self.n self n self.tt = 0 self.tt self tt 0 self.nt = [0] * self.n self.nt self nt [0] * self.n [0] 0 self.n self n self self M M n n def next(self, u: int):
        while self.nt[u] < self.n:
            v = self.nt[u]
            self.nt[u] = v + 1
            if self.M[u][v] != 0:
                return v
        return -1

     next while self.nt[u] < self.n:
            v = self.nt[u]
            self.nt[u] = v + 1
            if self.M[u][v] != 0:
                return v
         self.nt[u] < self.n self.nt[u] self.nt self nt u self.n self n v = self.nt[u] v self.nt[u] self.nt self nt u self.nt[u] = v + 1 self.nt[u] self.nt self nt u v + 1 v 1 if self.M[u][v] != 0:
                return v
         self.M[u][v] != 0 self.M[u][v] self.M[u] self.M self M u v 0 return v v return -1 -1 self self u: int u def dfs_visit(self, r: int):
        S = []                  # list をスタックとして使う
        S.append(r)
        self.color[r] = self.Color.GRAY
        self.tt += 1
        self.d[r] = self.tt
        while len(S) > 0:
            # スタックの一番上（= listの末尾）の値を取得 ※ pop()するとlistからなくなるので[-1]でアクセス
            u = S[-1]
            v = self.next(u)
            if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

     dfs_visit S = [] S [] S.append(r) S.append S append r self.color[r] = self.Color.GRAY self.color[r] self.color self color r self.Color.GRAY self.Color self Color GRAY self.tt += 1 self.tt self tt 1 self.d[r] = self.tt self.d[r] self.d self d r self.tt self tt while len(S) > 0:
            # スタックの一番上（= listの末尾）の値を取得 ※ pop()するとlistからなくなるので[-1]でアクセス
            u = S[-1]
            v = self.next(u)
            if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

     len(S) > 0 len(S) len S 0 u = S[-1] u S[-1] S -1 v = self.next(u) v self.next(u) self.next self next u if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

     v != -1 v -1 if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
             self.color[v] == self.Color.WHITE self.color[v] self.color self color v self.Color.WHITE self.Color self Color WHITE self.color[v] = self.Color.GRAY self.color[v] self.color self color v self.Color.GRAY self.Color self Color GRAY self.tt += 1 self.tt self tt 1 self.d[v] = self.tt self.d[v] self.d self d v self.tt self tt S.append(v) S.append S append v S.pop() S.pop S pop self.color[u] = self.Color.BLACK self.color[u] self.color self color u self.Color.BLACK self.Color self Color BLACK self.tt += 1 self.tt self tt 1 self.f[u] = self.tt self.f[u] self.f self f u self.tt self tt self self r: int r def dfs(self):
        for u in range(self.n):
            if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

        for i in range(self.n):
            print(f'{i+1} {self.d[i]} {self.f[i]}') dfs for u in range(self.n):
            if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

         u range(self.n) range self.n self n if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

         self.color[u] == self.Color.WHITE self.color[u] self.color self color u self.Color.WHITE self.Color self Color WHITE self.dfs_visit(u) self.dfs_visit self dfs_visit u for i in range(self.n):
            print(f'{i+1} {self.d[i]} {self.f[i]}') i range(self.n) range self.n self n print(f'{i+1} {self.d[i]} {self.f[i]}') print f'{i+1} {self.d[i]} {self.f[i]}' self self def main():
    n = int(input())
    M = [[0] * n for _ in range(n)]

    for i in range(n):
        line = input()
        inputs = [int(x) for x in line.split(' ')]
        u = inputs.pop(0)
        u -= 1                  # 0 オリジンへ変換
        _ = inputs.pop(0)       # k （使わない...）
        for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

    DFS(M, n).dfs() main n = int(input()) n int(input()) int input() input [0] * n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * n [0] 0 n M = [[0] * n for _ in range(n)] M [[0] * n for _ in range(n)] for i in range(n):
        line = input()
        inputs = [int(x) for x in line.split(' ')]
        u = inputs.pop(0)
        u -= 1                  # 0 オリジンへ変換
        _ = inputs.pop(0)       # k （使わない...）
        for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

     i range(n) range n line = input() line input() input int(x) for x in line.split(' ') for x in line.split(' ') x line.split(' ') line.split line split ' ' for x in line.split(' ') int(x) int x inputs = [int(x) for x in line.split(' ')] inputs [int(x) for x in line.split(' ')] u = inputs.pop(0) u inputs.pop(0) inputs.pop inputs pop 0 u -= 1 u 1 _ = inputs.pop(0) _ inputs.pop(0) inputs.pop inputs pop 0 for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

     v inputs v -= 1 v 1 M[u][v] = 1 M[u][v] [u] M u v 1 DFS(M, n).dfs() (M, n).dfs (M, n) DFS M n dfs if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    n = int(input())
    M = [[0] * n for _ in range(n)]

    for i in range(n):
        line = input()
        inputs = [int(x) for x in line.split(' ')]
        u = inputs.pop(0)
        u -= 1                  # 0 オリジンへ変換
        _ = inputs.pop(0)       # k （使わない...）
        for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

    DFS(M, n).dfs() def main():
    n = int(input())
    M = [[0] * n for _ in range(n)]

    for i in range(n):
        line = input()
        inputs = [int(x) for x in line.split(' ')]
        u = inputs.pop(0)
        u -= 1                  # 0 オリジンへ変換
        _ = inputs.pop(0)       # k （使わない...）
        for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

    DFS(M, n).dfs() main class DFS():
    class Color(Enum):
        WHITE = 0
        GRAY = 1
        BLACK = 2

    def __init__(self, M, n):
        self.M = M
        self.n = n
        self.color = [self.Color.WHITE for _ in range(self.n)]
        self.d, self.f = [-1] * self.n, [-1] * self.n
        self.tt = 0
        self.nt = [0] * self.n

    def next(self, u: int):
        while self.nt[u] < self.n:
            v = self.nt[u]
            self.nt[u] = v + 1
            if self.M[u][v] != 0:
                return v
        return -1

    def dfs_visit(self, r: int):
        S = []                  # list をスタックとして使う
        S.append(r)
        self.color[r] = self.Color.GRAY
        self.tt += 1
        self.d[r] = self.tt
        while len(S) > 0:
            # スタックの一番上（= listの末尾）の値を取得 ※ pop()するとlistからなくなるので[-1]でアクセス
            u = S[-1]
            v = self.next(u)
            if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

    def dfs(self):
        for u in range(self.n):
            if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

        for i in range(self.n):
            print(f'{i+1} {self.d[i]} {self.f[i]}') class DFS():
    class Color(Enum):
        WHITE = 0
        GRAY = 1
        BLACK = 2

    def __init__(self, M, n):
        self.M = M
        self.n = n
        self.color = [self.Color.WHITE for _ in range(self.n)]
        self.d, self.f = [-1] * self.n, [-1] * self.n
        self.tt = 0
        self.nt = [0] * self.n

    def next(self, u: int):
        while self.nt[u] < self.n:
            v = self.nt[u]
            self.nt[u] = v + 1
            if self.M[u][v] != 0:
                return v
        return -1

    def dfs_visit(self, r: int):
        S = []                  # list をスタックとして使う
        S.append(r)
        self.color[r] = self.Color.GRAY
        self.tt += 1
        self.d[r] = self.tt
        while len(S) > 0:
            # スタックの一番上（= listの末尾）の値を取得 ※ pop()するとlistからなくなるので[-1]でアクセス
            u = S[-1]
            v = self.next(u)
            if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

    def dfs(self):
        for u in range(self.n):
            if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

        for i in range(self.n):
            print(f'{i+1} {self.d[i]} {self.f[i]}') DFS
#!/usr/bin/env python3
from enum import Enum


class DFS():
    class Color(Enum):
        WHITE = 0
        GRAY = 1
        BLACK = 2

    def __init__(self, M, n):
        self.M = M
        self.n = n
        self.color = [self.Color.WHITE for _ in range(self.n)]
        self.d, self.f = [-1] * self.n, [-1] * self.n
        self.tt = 0
        self.nt = [0] * self.n

    def next(self, u: int):
        while self.nt[u] < self.n:
            v = self.nt[u]
            self.nt[u] = v + 1
            if self.M[u][v] != 0:
                return v
        return -1

    def dfs_visit(self, r: int):
        S = []                  # list をスタックとして使う
        S.append(r)
        self.color[r] = self.Color.GRAY
        self.tt += 1
        self.d[r] = self.tt
        while len(S) > 0:
            # スタックの一番上（= listの末尾）の値を取得 ※ pop()するとlistからなくなるので[-1]でアクセス
            u = S[-1]
            v = self.next(u)
            if v != -1:
                if self.color[v] == self.Color.WHITE:
                    self.color[v] = self.Color.GRAY
                    self.tt += 1
                    self.d[v] = self.tt
                    S.append(v)
            else:
                S.pop()
                self.color[u] = self.Color.BLACK
                self.tt += 1
                self.f[u] = self.tt

    def dfs(self):
        for u in range(self.n):
            if self.color[u] == self.Color.WHITE:
                self.dfs_visit(u)

        for i in range(self.n):
            print(f'{i+1} {self.d[i]} {self.f[i]}')


def main():
    n = int(input())
    M = [[0] * n for _ in range(n)]

    for i in range(n):
        line = input()
        inputs = [int(x) for x in line.split(' ')]
        u = inputs.pop(0)
        u -= 1                  # 0 オリジンへ変換
        _ = inputs.pop(0)       # k （使わない...）
        for v in inputs:
            v -= 1              # 0 オリジンへ変換
            M[u][v] = 1

    DFS(M, n).dfs()


if __name__ == '__main__':
    main()

