#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = (ll)(x)-1;0 <= i; i--)
#define reps(i,x) for(ll i = 1; i < (ll)(x)+1; i++)
#define rreps(i,x) for(ll i = (ll)(x); 1 <= i; i--)
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
const ll INF = numeric_limits<ll>::max()/4;
const int n_max = 1e5+10;

void print() {
    cout << endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail) != 0) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
void print(vector<T> &vec) {
    for (auto& a : vec) {
        cout << a;
        if (&a != &vec.back()) cout << '\n';
    }
    cout << endl;
}

template <class T>
void print(vector<vector<T>> &df) {
    for (auto& vec : df) {
        print(vec);
    }
}

template<class T, class U>
void print(pair<T,U> &p){
    print(p.first, p.second);
}


int main(){
    ll n,m; cin >> n >> m;
    vector<vector<ll>> graph(n);
    vector<ll> ans(n, INF), in(n, 0);

    rep(i,n - 1 + m) {
        ll a,b;cin >> a >> b;
        a--;b--;
        graph[a].emplace_back(b);
        // graph[b].emplace_back(a);
        in[b]++;
    }
    ll root;
    rep(i,n)if(in[i] == 0){
        root = i;
        ans[root] = 0;
    }
    // print(in);
    queue<Pll> que;
    que.push(Pll(root, 0));
    while(!que.empty()){
        Pll t = que.front();que.pop();
        for(auto to : graph[t.first]){
            if(in[to] == 1){
                ans[to] = t.first + 1;
                que.push(Pll(to, t.second + 1));
            }
            in[to]--;
        }
    }

    print(ans);
}
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;

class Graph {
public:
    int N;
    vector<vector<int>> G;
    vector<bool> visit;
    vector<int> cir;
    vector<int> exist;

    Graph(int N) : N(N) {
        G.resize(N);
        visit.resize(N, false);
        exist.resize(N, -1);
    }

    void add_edge(int s, int t){
        G[s].push_back(t);
    }

    bool dfs(int n, int s){
        if(n == s && visit[n]){
            return true;
        }
        if(visit[n]){
            return false;
        }


        visit[n] = true;
        for(int i = 0; i < (int)G[n].size(); i++){
            int nx = G[n][i];
            if(dfs(nx, s)){
                cir.push_back(n);
                return true;
            }
        }

        return false;
    }

    void solve(){
        //0 ~ N-1について自身に戻れるかdfsをN回
        bool found = false; //閉路があるか

        for(int i = 0; i < N; i++){
            fill(visit.begin(), visit.end(), false);
            if(dfs(i, i)){
                found = true;
                break;
            }
        }

        if(!found){
            cout << -1 << endl;
            return;
        }

        reverse(cir.begin(), cir.end());

        while(true){
            //debug
            /*
            cout << "cir = ";
            for(int val : cir){
                cout << val + 1 << " ";
            }
            cout << endl;
            */

            //閉路上の点かどうかを示すexist更新
            fill(exist.begin(), exist.end(), -1);
            for(int i = 0; i < (int)cir.size(); i++){
                exist[cir[i]] = i;
            }

            cir.push_back(cir[0]);

            bool check = false;
            for(int i = 0; i < (int)cir.size() - 1; i++){
                int n = cir[i];
                for(int j = 0; j < (int)G[n].size(); j++){
                    int nx = G[n][j];
                    if(exist[nx] >= 0 && nx != cir[i + 1]){
                        //閉路作り直し
                        check = true;

                        vector<int> tmp;
                        if(exist[n] < exist[nx]){
                            for(int k = 0; k < (int)cir.size() - 1; k++){
                                if(k <= exist[n] || exist[nx] <= k) tmp.push_back(cir[k]);
                            }
                        } else {
                            //cout << "hoge" << endl;
                            for(int k = exist[nx]; k <= exist[n]; k++){
                                tmp.push_back(cir[k]);
                            }
                        }

                        cir = tmp;
                        break;
                    }
                }

                if(check) break;
            }

            if(!check) break;
        }

        cout << cir.size() - 1 << endl;
        for(int i = 0; i < (int)cir.size() - 1; i++){
            cout << cir[i] + 1 << endl;
        }

        return;
    }
};

int main(){
    int N, M;
    cin >> N >> M;

    Graph gr = Graph(N);

    for(int i = 0; i < M; i++){
        int a, b;
        cin >> a >> b;
        gr.add_edge(a - 1, b - 1);
    }

    gr.solve();
    return 0;
}