from typing import List


def alds_1_11a():
    n = int(input())
    for i in range(n):
        u, k, *v = map(int, input().split())
        a = [0] * n
        for i_v in v:
            index_v = i_v - 1
            a[index_v] = 1
        print(*a)
    return


def print_matrices_list(matrices_list):
    for index_row in range(len(matrices_list)):
        print(*matrices_list[index_row])
    return


# def depth_first_search(u, stack, color, d):
#     stack.append(u)
#     color[u] = 1
#     while len(stack) != 0:
#     pass


def accept_adjacency_list() -> List[List[int]]:
    n = int(input())
    adjancy_list = []
    for i in range(n):
        list_u = list(map(int, input().split()))
        adjancy_list.append(list_u)
    return adjancy_list


def adjacency_list2adjacency_matrices(adj_list: List[List[int]]) -> List[List[int]]:
    adjancy_matrices = []
    n = len(adj_list)
    for list_u_i in adj_list:
        id_u, degree_k, *list_v = list_u_i
        list_row = [0] * n
        for v_i in list_v:
            list_row[v_i - 1] = 1
        adjancy_matrices.append(list_row)
    return adjancy_matrices


def dfs(adj_mat):
    n = len(adj_mat)
    list_colors = [0] * n  # 0:white, 1:gray, 2:black
    time = 0
    d = [0] * n  # 最初に訪問した頂点uの発見時刻
    f = [0] * n  # uの隣接リストを調べ終えた完了時刻

    for u in range(n):
        if list_colors[u] == 0:
            list_colors, time, d, f = dfs_visit(u, list_colors, adj_mat, d, f, time)

    for u in range(n):
        print(u + 1, d[u], f[u])
    return


def dfs_visit(u: int, list_colors: list, adj_mat: list, d: list, f: list, time: int):
    list_colors[u] = 1  # uを訪問
    time += 1
    d[u] = time
    for v in range(len(list_colors)):  # v:次に探索する場所
        if adj_mat[u][v] == 0:  # uからvに行く辺がないなら次へ
            continue
        if list_colors[v] == 0:  # uからvに道があるかつ行ったことがないなら行く。
            list_colors, time, d, f = dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            )
    list_colors[u] = 2
    time += 1
    f[u] = time
    return list_colors, time, d, f


if __name__ == "__main__":
    # init
    adjacency_list = accept_adjacency_list()
    adjacency_matirces = adjacency_list2adjacency_matrices(adj_list=adjacency_list)

    dfs(adj_mat=adjacency_matirces)
 from typing import List def alds_1_11a():
    n = int(input())
    for i in range(n):
        u, k, *v = map(int, input().split())
        a = [0] * n
        for i_v in v:
            index_v = i_v - 1
            a[index_v] = 1
        print(*a)
    return alds_1_11a n = int(input()) n int(input()) int input() input for i in range(n):
        u, k, *v = map(int, input().split())
        a = [0] * n
        for i_v in v:
            index_v = i_v - 1
            a[index_v] = 1
        print(*a)
     i range(n) range n u, k, *v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *v v a = [0] * n a [0] * n [0] 0 n for i_v in v:
            index_v = i_v - 1
            a[index_v] = 1
         i_v v index_v = i_v - 1 index_v i_v - 1 i_v 1 a[index_v] = 1 a[index_v] a index_v 1 print(*a) print *a a return def print_matrices_list(matrices_list):
    for index_row in range(len(matrices_list)):
        print(*matrices_list[index_row])
    return


# def depth_first_search(u, stack, color, d):
#     stack.append(u)
#     color[u] = 1
#     while len(stack) != 0:
#     pass print_matrices_list for index_row in range(len(matrices_list)):
        print(*matrices_list[index_row])
     index_row range(len(matrices_list)) range len(matrices_list) len matrices_list print(*matrices_list[index_row]) print *matrices_list[index_row] matrices_list[index_row] matrices_list index_row return matrices_list matrices_list def accept_adjacency_list() -> List[List[int]]:
    n = int(input())
    adjancy_list = []
    for i in range(n):
        list_u = list(map(int, input().split()))
        adjancy_list.append(list_u)
    return adjancy_list accept_adjacency_list n = int(input()) n int(input()) int input() input adjancy_list = [] adjancy_list [] for i in range(n):
        list_u = list(map(int, input().split()))
        adjancy_list.append(list_u)
     i range(n) range n list_u = list(map(int, input().split())) list_u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split adjancy_list.append(list_u) adjancy_list.append adjancy_list append list_u return adjancy_list adjancy_list def adjacency_list2adjacency_matrices(adj_list: List[List[int]]) -> List[List[int]]:
    adjancy_matrices = []
    n = len(adj_list)
    for list_u_i in adj_list:
        id_u, degree_k, *list_v = list_u_i
        list_row = [0] * n
        for v_i in list_v:
            list_row[v_i - 1] = 1
        adjancy_matrices.append(list_row)
    return adjancy_matrices adjacency_list2adjacency_matrices adjancy_matrices = [] adjancy_matrices [] n = len(adj_list) n len(adj_list) len adj_list for list_u_i in adj_list:
        id_u, degree_k, *list_v = list_u_i
        list_row = [0] * n
        for v_i in list_v:
            list_row[v_i - 1] = 1
        adjancy_matrices.append(list_row)
     list_u_i adj_list id_u, degree_k, *list_v = list_u_i id_u list_u_i degree_k *list_v list_v list_row = [0] * n list_row [0] * n [0] 0 n for v_i in list_v:
            list_row[v_i - 1] = 1
         v_i list_v list_row[v_i - 1] = 1 list_row[v_i - 1] list_row v_i - 1 v_i 1 1 adjancy_matrices.append(list_row) adjancy_matrices.append adjancy_matrices append list_row return adjancy_matrices adjancy_matrices adj_list: List[List[int]] adj_list def dfs(adj_mat):
    n = len(adj_mat)
    list_colors = [0] * n  # 0:white, 1:gray, 2:black
    time = 0
    d = [0] * n  # 最初に訪問した頂点uの発見時刻
    f = [0] * n  # uの隣接リストを調べ終えた完了時刻

    for u in range(n):
        if list_colors[u] == 0:
            list_colors, time, d, f = dfs_visit(u, list_colors, adj_mat, d, f, time)

    for u in range(n):
        print(u + 1, d[u], f[u])
    return dfs n = len(adj_mat) n len(adj_mat) len adj_mat list_colors = [0] * n list_colors [0] * n [0] 0 n time = 0 time 0 d = [0] * n d [0] * n [0] 0 n f = [0] * n f [0] * n [0] 0 n for u in range(n):
        if list_colors[u] == 0:
            list_colors, time, d, f = dfs_visit(u, list_colors, adj_mat, d, f, time)

     u range(n) range n if list_colors[u] == 0:
            list_colors, time, d, f = dfs_visit(u, list_colors, adj_mat, d, f, time)

     list_colors[u] == 0 list_colors[u] list_colors u 0 list_colors, time, d, f = dfs_visit(u, list_colors, adj_mat, d, f, time) list_colors dfs_visit(u, list_colors, adj_mat, d, f, time) dfs_visit u list_colors adj_mat d f time time d f for u in range(n):
        print(u + 1, d[u], f[u])
     u range(n) range n print(u + 1, d[u], f[u]) print u + 1 u 1 d[u] d u f[u] f u return adj_mat adj_mat def dfs_visit(u: int, list_colors: list, adj_mat: list, d: list, f: list, time: int):
    list_colors[u] = 1  # uを訪問
    time += 1
    d[u] = time
    for v in range(len(list_colors)):  # v:次に探索する場所
        if adj_mat[u][v] == 0:  # uからvに行く辺がないなら次へ
            continue
        if list_colors[v] == 0:  # uからvに道があるかつ行ったことがないなら行く。
            list_colors, time, d, f = dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            )
    list_colors[u] = 2
    time += 1
    f[u] = time
    return list_colors, time, d, f dfs_visit list_colors[u] = 1 list_colors[u] list_colors u 1 time += 1 time 1 d[u] = time d[u] d u time for v in range(len(list_colors)):  # v:次に探索する場所
        if adj_mat[u][v] == 0:  # uからvに行く辺がないなら次へ
            continue
        if list_colors[v] == 0:  # uからvに道があるかつ行ったことがないなら行く。
            list_colors, time, d, f = dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            )
     v range(len(list_colors)) range len(list_colors) len list_colors if adj_mat[u][v] == 0:  # uからvに行く辺がないなら次へ
            continue
         adj_mat[u][v] == 0 adj_mat[u][v] [u] adj_mat u v 0 continue if list_colors[v] == 0:  # uからvに道があるかつ行ったことがないなら行く。
            list_colors, time, d, f = dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            )
     list_colors[v] == 0 list_colors[v] list_colors v 0 list_colors, time, d, f = dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            ) list_colors dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            ) dfs_visit u=v u v list_colors=list_colors list_colors list_colors adj_mat=adj_mat adj_mat adj_mat d=d d d f=f f f time=time time time time d f list_colors[u] = 2 list_colors[u] list_colors u 2 time += 1 time 1 f[u] = time f[u] f u time return list_colors, time, d, f return list_colors, time, d, f list_colors time d f u: int u list_colors: list list_colors adj_mat: list adj_mat d: list d f: list f time: int time if __name__ == "__main__":
    # init
    adjacency_list = accept_adjacency_list()
    adjacency_matirces = adjacency_list2adjacency_matrices(adj_list=adjacency_list)

    dfs(adj_mat=adjacency_matirces) __name__ == "__main__" __name__ "__main__" adjacency_list = accept_adjacency_list() adjacency_list accept_adjacency_list() accept_adjacency_list adjacency_matirces = adjacency_list2adjacency_matrices(adj_list=adjacency_list) adjacency_matirces adjacency_list2adjacency_matrices(adj_list=adjacency_list) adjacency_list2adjacency_matrices adj_list=adjacency_list adj_list adjacency_list dfs(adj_mat=adjacency_matirces) dfs adj_mat=adjacency_matirces adj_mat adjacency_matirces def accept_adjacency_list() -> List[List[int]]:
    n = int(input())
    adjancy_list = []
    for i in range(n):
        list_u = list(map(int, input().split()))
        adjancy_list.append(list_u)
    return adjancy_list def accept_adjacency_list() -> List[List[int]]:
    n = int(input())
    adjancy_list = []
    for i in range(n):
        list_u = list(map(int, input().split()))
        adjancy_list.append(list_u)
    return adjancy_list accept_adjacency_list adjacency_matirces = adjacency_list2adjacency_matrices(adj_list=adjacency_list) adjacency_list2adjacency_matrices(adj_list=adjacency_list) adjacency_matirces def alds_1_11a():
    n = int(input())
    for i in range(n):
        u, k, *v = map(int, input().split())
        a = [0] * n
        for i_v in v:
            index_v = i_v - 1
            a[index_v] = 1
        print(*a)
    return def alds_1_11a():
    n = int(input())
    for i in range(n):
        u, k, *v = map(int, input().split())
        a = [0] * n
        for i_v in v:
            index_v = i_v - 1
            a[index_v] = 1
        print(*a)
    return alds_1_11a def adjacency_list2adjacency_matrices(adj_list: List[List[int]]) -> List[List[int]]:
    adjancy_matrices = []
    n = len(adj_list)
    for list_u_i in adj_list:
        id_u, degree_k, *list_v = list_u_i
        list_row = [0] * n
        for v_i in list_v:
            list_row[v_i - 1] = 1
        adjancy_matrices.append(list_row)
    return adjancy_matrices def adjacency_list2adjacency_matrices(adj_list: List[List[int]]) -> List[List[int]]:
    adjancy_matrices = []
    n = len(adj_list)
    for list_u_i in adj_list:
        id_u, degree_k, *list_v = list_u_i
        list_row = [0] * n
        for v_i in list_v:
            list_row[v_i - 1] = 1
        adjancy_matrices.append(list_row)
    return adjancy_matrices adjacency_list2adjacency_matrices def dfs(adj_mat):
    n = len(adj_mat)
    list_colors = [0] * n  # 0:white, 1:gray, 2:black
    time = 0
    d = [0] * n  # 最初に訪問した頂点uの発見時刻
    f = [0] * n  # uの隣接リストを調べ終えた完了時刻

    for u in range(n):
        if list_colors[u] == 0:
            list_colors, time, d, f = dfs_visit(u, list_colors, adj_mat, d, f, time)

    for u in range(n):
        print(u + 1, d[u], f[u])
    return def dfs(adj_mat):
    n = len(adj_mat)
    list_colors = [0] * n  # 0:white, 1:gray, 2:black
    time = 0
    d = [0] * n  # 最初に訪問した頂点uの発見時刻
    f = [0] * n  # uの隣接リストを調べ終えた完了時刻

    for u in range(n):
        if list_colors[u] == 0:
            list_colors, time, d, f = dfs_visit(u, list_colors, adj_mat, d, f, time)

    for u in range(n):
        print(u + 1, d[u], f[u])
    return dfs adjacency_list = accept_adjacency_list() accept_adjacency_list() adjacency_list def print_matrices_list(matrices_list):
    for index_row in range(len(matrices_list)):
        print(*matrices_list[index_row])
    return


# def depth_first_search(u, stack, color, d):
#     stack.append(u)
#     color[u] = 1
#     while len(stack) != 0:
#     pass def print_matrices_list(matrices_list):
    for index_row in range(len(matrices_list)):
        print(*matrices_list[index_row])
    return


# def depth_first_search(u, stack, color, d):
#     stack.append(u)
#     color[u] = 1
#     while len(stack) != 0:
#     pass print_matrices_list def dfs_visit(u: int, list_colors: list, adj_mat: list, d: list, f: list, time: int):
    list_colors[u] = 1  # uを訪問
    time += 1
    d[u] = time
    for v in range(len(list_colors)):  # v:次に探索する場所
        if adj_mat[u][v] == 0:  # uからvに行く辺がないなら次へ
            continue
        if list_colors[v] == 0:  # uからvに道があるかつ行ったことがないなら行く。
            list_colors, time, d, f = dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            )
    list_colors[u] = 2
    time += 1
    f[u] = time
    return list_colors, time, d, f def dfs_visit(u: int, list_colors: list, adj_mat: list, d: list, f: list, time: int):
    list_colors[u] = 1  # uを訪問
    time += 1
    d[u] = time
    for v in range(len(list_colors)):  # v:次に探索する場所
        if adj_mat[u][v] == 0:  # uからvに行く辺がないなら次へ
            continue
        if list_colors[v] == 0:  # uからvに道があるかつ行ったことがないなら行く。
            list_colors, time, d, f = dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            )
    list_colors[u] = 2
    time += 1
    f[u] = time
    return list_colors, time, d, f dfs_visit
from typing import List


def alds_1_11a():
    n = int(input())
    for i in range(n):
        u, k, *v = map(int, input().split())
        a = [0] * n
        for i_v in v:
            index_v = i_v - 1
            a[index_v] = 1
        print(*a)
    return


def print_matrices_list(matrices_list):
    for index_row in range(len(matrices_list)):
        print(*matrices_list[index_row])
    return


# def depth_first_search(u, stack, color, d):
#     stack.append(u)
#     color[u] = 1
#     while len(stack) != 0:
#     pass


def accept_adjacency_list() -> List[List[int]]:
    n = int(input())
    adjancy_list = []
    for i in range(n):
        list_u = list(map(int, input().split()))
        adjancy_list.append(list_u)
    return adjancy_list


def adjacency_list2adjacency_matrices(adj_list: List[List[int]]) -> List[List[int]]:
    adjancy_matrices = []
    n = len(adj_list)
    for list_u_i in adj_list:
        id_u, degree_k, *list_v = list_u_i
        list_row = [0] * n
        for v_i in list_v:
            list_row[v_i - 1] = 1
        adjancy_matrices.append(list_row)
    return adjancy_matrices


def dfs(adj_mat):
    n = len(adj_mat)
    list_colors = [0] * n  # 0:white, 1:gray, 2:black
    time = 0
    d = [0] * n  # 最初に訪問した頂点uの発見時刻
    f = [0] * n  # uの隣接リストを調べ終えた完了時刻

    for u in range(n):
        if list_colors[u] == 0:
            list_colors, time, d, f = dfs_visit(u, list_colors, adj_mat, d, f, time)

    for u in range(n):
        print(u + 1, d[u], f[u])
    return


def dfs_visit(u: int, list_colors: list, adj_mat: list, d: list, f: list, time: int):
    list_colors[u] = 1  # uを訪問
    time += 1
    d[u] = time
    for v in range(len(list_colors)):  # v:次に探索する場所
        if adj_mat[u][v] == 0:  # uからvに行く辺がないなら次へ
            continue
        if list_colors[v] == 0:  # uからvに道があるかつ行ったことがないなら行く。
            list_colors, time, d, f = dfs_visit(
                u=v, list_colors=list_colors, adj_mat=adj_mat, d=d, f=f, time=time
            )
    list_colors[u] = 2
    time += 1
    f[u] = time
    return list_colors, time, d, f


if __name__ == "__main__":
    # init
    adjacency_list = accept_adjacency_list()
    adjacency_matirces = adjacency_list2adjacency_matrices(adj_list=adjacency_list)

    dfs(adj_mat=adjacency_matirces)

