#include <iostream>
#include <vector>
#include <map>
using namespace std;
map<int, int> parent;
void printCycle(int node, int child){
if(node != child)
printCycle(parent[node], child);
cout << node <<' ';
}
int timer = 0;
void dfs(int node, vector<vector<int>> &graph, vector<pair<int, int>> &time, map<pair<int, int>, string> &type){
time[node].first = ++timer;
for(int child: graph[node]){
parent[child] = node;
if(!time[child].first){
type[{node, child}] = "Tree";
dfs(child, graph, time, type);
}
else if(!time[child].second)
type[{node, child}] = "Back";
else if(time[node].first < time[child].first )
type[{node, child}] = "Forward";
else
type[{node, child}] = "Cross";
}
time[node].second = ++timer;
}
using namespace std;
int main(){
int nodes, egdes;
cin >> nodes >> egdes;
vector<vector<int>> graph(nodes+1);
vector<pair<int, int>> time(nodes+1); // Arrival & Departure
map<pair<int, int>, string> type;
int from, to;
while(egdes--){
cin >> from >> to;
graph[from].push_back(to);
}
// Show Graph
cout << endl ;
for(int node = 1; node <= nodes; node++){
cout << node <<" : ";
for(int child: graph[node])
cout << child <<' ';
cout << endl;
}
// dfs
cout << endl;
for(int node = 1; node <= nodes; node++)
if(!time[node].first)
dfs(node, graph, time, type);
// cycle paths
cout << endl;
for(int node = 1; node <= nodes; node++)
for(auto child: graph[node])
if(type[{node, child}] == "Back"){
printCycle(node, child);
cout << child << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bWFwPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwptYXA8aW50LCBpbnQ+IHBhcmVudDsKdm9pZCBwcmludEN5Y2xlKGludCBub2RlLCBpbnQgY2hpbGQpewogICAgaWYobm9kZSAhPSBjaGlsZCkKICAgICAgICBwcmludEN5Y2xlKHBhcmVudFtub2RlXSwgY2hpbGQpOwoKICAgIGNvdXQgPDwgbm9kZSA8PCcgJzsKfQppbnQgdGltZXIgPSAwOwp2b2lkIGRmcyhpbnQgbm9kZSwgdmVjdG9yPHZlY3RvcjxpbnQ+PiAmZ3JhcGgsI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XMtLSl7CiAgICAgICAgY2luID4+IGZyb20gPj4gdG87CiAgICAgICAgZ3JhcGhbZnJvbV0ucHVzaF9iYWNrKHRvKTsKICAgIH0KCiAgICAvLyBTaG93IEdyYXBoCiAgICBjb3V0IDw8IGVuZGwgOwogICAgZm9yKGludCBub2RlID0gMTsgbm9kZSA8PSBub2Rlczsgbm9kZSsrKXsKICAgICAgICBjb3V0IDw8IG5vZGUgPDwiIDogIjsKICAgICAgICBmb3IoaW50IGNoaWxkOiBncmFwaFtub2RlXSkKICAgICAgICAgICAgY291dCA8PCBjaGlsZCA8PCcgJzsKICAgICAgICBjb3V0IDw8IGVuZGw7CiAgICB9CiAgICAvLyBkZnMKICAgIGNvdXQgPDwgZW5kbDsKICAgIGZvcihpbnQgbm9kZSA9IDE7IG5vZGUgPD0gbm9kZXM7IG5vZGUrKykKICAgICAgICBpZighdGltZVtub2RlXS5maXJzdCkKICAgICAgICAgICAgZGZzKG5vZGUsIGdyYXBoLCB0aW1lLCB0eXBlKTsKCiAgICAvLyBjeWNsZSBwYXRocwogICAgY291dCA8PCBlbmRsOwogICAgZm9yKGludCBub2RlID0gMTsgbm9kZSA8PSBub2Rlczsgbm9kZSsrKQogICAgICAgIGZvcihhdXRvIGNoaWxkOiBncmFwaFtub2RlXSkKICAgICAgICAgICAgaWYodHlwZVt7bm9kZSwgY2hpbGR9XSA9PSAiQmFjayIpewogICAgICAgICAgICAgICAgcHJpbnRDeWNsZShub2RlLCBjaGlsZCk7CiAgICAgICAgICAgICAgICBjb3V0IDw8IGNoaWxkIDw8IGVuZGw7CiAgICAgICAgICAgIH0KCiAgICByZXR1cm4gMDsKfQo=