#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;
}
