from collections import OrderedDict

class Stack():
    def __init__(self):
        self.el = []
    def add(self, el):
        self.el.append(el)
    def pop(self):
        return self.el.pop()
    def top(self):
        return self.el[-1]
    def isEmpty(self):
        if self.el:
            return False
        return True

class Node():
    def __init__(self, id, edge=[], flag=0, discover=0, finish=0):
        self.id = id
        self.edge = edge
        self.flag = flag
        self.discover = discover
        self.finish = finish
    def show(self):
        print self.id, self.discover, self.finish
        
def notVisited(dic):
    for i in dic.keys():
        if dic[i].flag != 2:
            return i
    return None
        
n = int(raw_input())
dic = {}
for i in range(n):
    tmp = map(int, raw_input().split())
    if tmp[1] > 0:
        dic[tmp[0]] = Node(tmp[0], tmp[2:])
    else:
        dic[tmp[0]] = Node(tmp[0])
dic = OrderedDict(dic)

start = 1
time = 1
st = Stack()
dic[start].flag = 1
dic[start].discover = time
st.add(dic[start])

while not st.isEmpty():
    time += 1
    u = st.top()
    v = u.edge
    if v:
        for i in range(len(v)):
            if dic[v[i]].flag == 0:
                dic[v[i]].flag = 1
                dic[v[i]].discover = time
                if i != len(v)-1:
                    dic[u.id].edge = u.edge[i+1:]
                else:
                    dic[u.id].edge = []
                st.add(dic[v[i]])
                break
        else:
            st.pop()
            dic[u.id].flag = 2
            dic[u.id].finish = time
    else:
        st.pop()    
        dic[u.id].flag = 2
        dic[u.id].finish = time
    if st.isEmpty() and notVisited(dic):
        new = notVisited(dic)
        dic[new].flag = 1
        time += 1
        dic[new].discover = time
        st.add(dic[new])
for i in dic.keys():
    dic[i].show() from collections import OrderedDict class Stack():
    def __init__(self):
        self.el = []
    def add(self, el):
        self.el.append(el)
    def pop(self):
        return self.el.pop()
    def top(self):
        return self.el[-1]
    def isEmpty(self):
        if self.el:
            return False
        return True Stack def __init__(self):
        self.el = []
     __init__ self.el = [] self.el self el [] self self def add(self, el):
        self.el.append(el)
     add self.el.append(el) self.el.append self.el self el append el self self el el def pop(self):
        return self.el.pop()
     pop return self.el.pop() self.el.pop() self.el.pop self.el self el pop self self def top(self):
        return self.el[-1]
     top return self.el[-1] self.el[-1] self.el self el -1 self self def isEmpty(self):
        if self.el:
            return False
        return True isEmpty if self.el:
            return False
         self.el self el return False False return True True self self class Node():
    def __init__(self, id, edge=[], flag=0, discover=0, finish=0):
        self.id = id
        self.edge = edge
        self.flag = flag
        self.discover = discover
        self.finish = finish
    def show(self):
        print self.id, self.discover, self.finish
         Node def __init__(self, id, edge=[], flag=0, discover=0, finish=0):
        self.id = id
        self.edge = edge
        self.flag = flag
        self.discover = discover
        self.finish = finish
     __init__ self.id = id self.id self id id self.edge = edge self.edge self edge edge self.flag = flag self.flag self flag flag self.discover = discover self.discover self discover discover self.finish = finish self.finish self finish finish self self id id edge=[] edge [] flag=0 flag 0 discover=0 discover 0 finish=0 finish 0 def show(self):
        print self.id, self.discover, self.finish
         show print self.id self id self.discover self discover self.finish self finish self self def notVisited(dic):
    for i in dic.keys():
        if dic[i].flag != 2:
            return i
    return None
         notVisited for i in dic.keys():
        if dic[i].flag != 2:
            return i
     i dic.keys() dic.keys dic keys if dic[i].flag != 2:
            return i
     dic[i].flag != 2 dic[i].flag [i] dic i flag 2 return i i return None None dic dic n = int(raw_input()) n int(raw_input()) int raw_input() raw_input dic = {} dic {} for i in range(n):
    tmp = map(int, raw_input().split())
    if tmp[1] > 0:
        dic[tmp[0]] = Node(tmp[0], tmp[2:])
    else:
        dic[tmp[0]] = Node(tmp[0]) i range(n) range n tmp = map(int, raw_input().split()) tmp map(int, raw_input().split()) map int raw_input().split() ().split () raw_input split if tmp[1] > 0:
        dic[tmp[0]] = Node(tmp[0], tmp[2:])
    else:
        dic[tmp[0]] = Node(tmp[0]) tmp[1] > 0 tmp[1] tmp 1 0 dic[tmp[0]] = Node(tmp[0], tmp[2:]) dic[tmp[0]] dic tmp[0] tmp 0 Node(tmp[0], tmp[2:]) Node tmp[0] tmp 0 tmp[2:] tmp 2: 2 dic[tmp[0]] = Node(tmp[0]) dic[tmp[0]] dic tmp[0] tmp 0 Node(tmp[0]) Node tmp[0] tmp 0 dic = OrderedDict(dic) dic OrderedDict(dic) OrderedDict dic start = 1 start 1 time = 1 time 1 st = Stack() st Stack() Stack dic[start].flag = 1 dic[start].flag [start] dic start flag 1 dic[start].discover = time dic[start].discover [start] dic start discover time st.add(dic[start]) st.add st add dic[start] dic start while not st.isEmpty():
    time += 1
    u = st.top()
    v = u.edge
    if v:
        for i in range(len(v)):
            if dic[v[i]].flag == 0:
                dic[v[i]].flag = 1
                dic[v[i]].discover = time
                if i != len(v)-1:
                    dic[u.id].edge = u.edge[i+1:]
                else:
                    dic[u.id].edge = []
                st.add(dic[v[i]])
                break
        else:
            st.pop()
            dic[u.id].flag = 2
            dic[u.id].finish = time
    else:
        st.pop()    
        dic[u.id].flag = 2
        dic[u.id].finish = time
    if st.isEmpty() and notVisited(dic):
        new = notVisited(dic)
        dic[new].flag = 1
        time += 1
        dic[new].discover = time
        st.add(dic[new]) not st.isEmpty() st.isEmpty() st.isEmpty st isEmpty time += 1 time 1 u = st.top() u st.top() st.top st top v = u.edge v u.edge u edge if v:
        for i in range(len(v)):
            if dic[v[i]].flag == 0:
                dic[v[i]].flag = 1
                dic[v[i]].discover = time
                if i != len(v)-1:
                    dic[u.id].edge = u.edge[i+1:]
                else:
                    dic[u.id].edge = []
                st.add(dic[v[i]])
                break
        else:
            st.pop()
            dic[u.id].flag = 2
            dic[u.id].finish = time
    else:
        st.pop()    
        dic[u.id].flag = 2
        dic[u.id].finish = time
     v for i in range(len(v)):
            if dic[v[i]].flag == 0:
                dic[v[i]].flag = 1
                dic[v[i]].discover = time
                if i != len(v)-1:
                    dic[u.id].edge = u.edge[i+1:]
                else:
                    dic[u.id].edge = []
                st.add(dic[v[i]])
                break
        else:
            st.pop()
            dic[u.id].flag = 2
            dic[u.id].finish = time
     i range(len(v)) range len(v) len v if dic[v[i]].flag == 0:
                dic[v[i]].flag = 1
                dic[v[i]].discover = time
                if i != len(v)-1:
                    dic[u.id].edge = u.edge[i+1:]
                else:
                    dic[u.id].edge = []
                st.add(dic[v[i]])
                break
         dic[v[i]].flag == 0 dic[v[i]].flag [v[i]] dic v[i] v i flag 0 dic[v[i]].flag = 1 dic[v[i]].flag [v[i]] dic v[i] v i flag 1 dic[v[i]].discover = time dic[v[i]].discover [v[i]] dic v[i] v i discover time if i != len(v)-1:
                    dic[u.id].edge = u.edge[i+1:]
                else:
                    dic[u.id].edge = []
                 i != len(v)-1 i len(v)-1 len(v) len v 1 dic[u.id].edge = u.edge[i+1:] dic[u.id].edge [u.id] dic u.id u id edge u.edge[i+1:] u.edge u edge i+1: i+1 i 1 dic[u.id].edge = [] dic[u.id].edge [u.id] dic u.id u id edge [] st.add(dic[v[i]]) st.add st add dic[v[i]] dic v[i] v i break st.pop() st.pop st pop dic[u.id].flag = 2 dic[u.id].flag [u.id] dic u.id u id flag 2 dic[u.id].finish = time dic[u.id].finish [u.id] dic u.id u id finish time st.pop() st.pop st pop dic[u.id].flag = 2 dic[u.id].flag [u.id] dic u.id u id flag 2 dic[u.id].finish = time dic[u.id].finish [u.id] dic u.id u id finish time if st.isEmpty() and notVisited(dic):
        new = notVisited(dic)
        dic[new].flag = 1
        time += 1
        dic[new].discover = time
        st.add(dic[new]) st.isEmpty() and notVisited(dic) st.isEmpty() st.isEmpty st isEmpty notVisited(dic) notVisited dic new = notVisited(dic) new notVisited(dic) notVisited dic dic[new].flag = 1 dic[new].flag [new] dic new flag 1 time += 1 time 1 dic[new].discover = time dic[new].discover [new] dic new discover time st.add(dic[new]) st.add st add dic[new] dic new for i in dic.keys():
    dic[i].show() i dic.keys() dic.keys dic keys dic[i].show() [i].show [i] dic i show dic = OrderedDict(dic) OrderedDict(dic) dic n = int(raw_input()) int(raw_input()) n start = 1 1 start v = u.edge u.edge v time = 1 1 time time += 1 1 time dic = {} {} dic def notVisited(dic):
    for i in dic.keys():
        if dic[i].flag != 2:
            return i
    return None
         def notVisited(dic):
    for i in dic.keys():
        if dic[i].flag != 2:
            return i
    return None
         notVisited class Node():
    def __init__(self, id, edge=[], flag=0, discover=0, finish=0):
        self.id = id
        self.edge = edge
        self.flag = flag
        self.discover = discover
        self.finish = finish
    def show(self):
        print self.id, self.discover, self.finish
         class Node():
    def __init__(self, id, edge=[], flag=0, discover=0, finish=0):
        self.id = id
        self.edge = edge
        self.flag = flag
        self.discover = discover
        self.finish = finish
    def show(self):
        print self.id, self.discover, self.finish
         Node tmp = map(int, raw_input().split()) map(int, raw_input().split()) tmp time += 1 1 time class Stack():
    def __init__(self):
        self.el = []
    def add(self, el):
        self.el.append(el)
    def pop(self):
        return self.el.pop()
    def top(self):
        return self.el[-1]
    def isEmpty(self):
        if self.el:
            return False
        return True class Stack():
    def __init__(self):
        self.el = []
    def add(self, el):
        self.el.append(el)
    def pop(self):
        return self.el.pop()
    def top(self):
        return self.el[-1]
    def isEmpty(self):
        if self.el:
            return False
        return True Stack st = Stack() Stack() st u = st.top() st.top() u new = notVisited(dic) notVisited(dic) new
from collections import OrderedDict

class Stack():
    def __init__(self):
        self.el = []
    def add(self, el):
        self.el.append(el)
    def pop(self):
        return self.el.pop()
    def top(self):
        return self.el[-1]
    def isEmpty(self):
        if self.el:
            return False
        return True

class Node():
    def __init__(self, id, edge=[], flag=0, discover=0, finish=0):
        self.id = id
        self.edge = edge
        self.flag = flag
        self.discover = discover
        self.finish = finish
    def show(self):
        print self.id, self.discover, self.finish
        
def notVisited(dic):
    for i in dic.keys():
        if dic[i].flag != 2:
            return i
    return None
        
n = int(raw_input())
dic = {}
for i in range(n):
    tmp = map(int, raw_input().split())
    if tmp[1] > 0:
        dic[tmp[0]] = Node(tmp[0], tmp[2:])
    else:
        dic[tmp[0]] = Node(tmp[0])
dic = OrderedDict(dic)

start = 1
time = 1
st = Stack()
dic[start].flag = 1
dic[start].discover = time
st.add(dic[start])

while not st.isEmpty():
    time += 1
    u = st.top()
    v = u.edge
    if v:
        for i in range(len(v)):
            if dic[v[i]].flag == 0:
                dic[v[i]].flag = 1
                dic[v[i]].discover = time
                if i != len(v)-1:
                    dic[u.id].edge = u.edge[i+1:]
                else:
                    dic[u.id].edge = []
                st.add(dic[v[i]])
                break
        else:
            st.pop()
            dic[u.id].flag = 2
            dic[u.id].finish = time
    else:
        st.pop()    
        dic[u.id].flag = 2
        dic[u.id].finish = time
    if st.isEmpty() and notVisited(dic):
        new = notVisited(dic)
        dic[new].flag = 1
        time += 1
        dic[new].discover = time
        st.add(dic[new])
for i in dic.keys():
    dic[i].show()