/// Code By t_bone_289 (Tushar Singh)
/// An Immature Programmer.... I'm still developing !!
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pll pair<ll,ll>
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define endl '\n'
#define M 1000000007
#define error(x) cout << #x << " = " << x << endl;
#define pi 3.14159265358979323846
typedef long long ll;
typedef long double ld;
template <typename T>
void ContainerError(T &v){cout<<"{";for(auto &x:v){cout<<x<<',';}cout<<"}"<<endl;return;}
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}
ll mpow(ll base,ll exp){ll res=1;while(exp>0){if(exp%2==1) res=(res*base);base=(base*base);exp/=2;}return (res);}
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
bool dfs(ll ver,vector<set<ll> > &G,vector<ll> &parent,vector<ll> &vis,ll &st,ll &en)
{
    vis[ver] = 1;
    for(auto &x:G[ver])
    {
        if(vis[x]==0)
        {
            parent[x] = ver;
            bool flag = dfs(x,G,parent,vis,st,en);
            if(flag)
            {
                return true;
            }
        }
        else if(vis[x]==1)
        {
            st = x;
            en = ver;
            return true;
        }
    }
    vis[ver]=2;
    return 0;
}
void clean(vector<set<ll> > &G,set<ll> &s,ll n)
{
    for(ll i=0;i<n;i++)
    {
        if(s.find(i)==s.end())
        {
            continue;
        }
        set<ll> s2;
        for(auto &y:G[i])
        {
            if(s.find(y)!=s.end())
            {
                s2.insert(y);
            }
        }
        G[i].clear();
        for(auto &x:s2)
        {
            G[i].insert(x);
        }
        s2.clear();
    }
    return;
}
void solve(vector<ll> &ans,vector<set<ll> > &G,set<ll> &s,ll n,ll N)
{
    if(N<=3)
    {
        return;
    }
    map<ll,ll> m;
    for(ll i=0;i<N;i++)
    {
        m[ans[i]]=i;
    }
    for(ll i=0;i<N;i++)
    {
        if(G[ans[i]].size() > 1)
        {
            ll ne = ans[(i+1)%N];
            auto it = G[ans[i]].begin();
            if((*it)==ne)
            {
                it++;
            }
            ll val =  (*it);
//            error(ans[i]);
//            error(val);
            ll in = m[val];
//            error(in);
            vector<ll> ans2;
            set<ll> s2;
            if(in > i)
            {
                ll j = in;
                while(j!=i)
                {
                    ans2.pb(ans[j]);
                    s2.insert(ans[j]);
                    j = (j+1)%N;
                }
                ans2.pb(ans[j]);
                s2.insert(ans[j]);

            }
            else
            {
                for(ll j=in;j<=i;j++)
                {
                    ans2.pb(ans[j]);
                    s2.insert(ans[j]);
                }
            }
            clean(G,s2,n);
            solve(ans2,G,s2,n,ans2.size());
            ans = ans2;
            return;
        }
    }
    return;
}
int main()
{
//    ios_base::sync_with_stdio(0);
//    cin.tie(0);
//    cout.tie(0);
    ll n,m;
    cin>>n>>m;
    vector<set<ll> > G(n);
    for(ll i=0;i<m;i++)
    {
        ll a,b;
        cin>>a>>b;
        a--;
        b--;
        G[a].insert(b);
    }
    vector<ll> visited(n,0);
    vector<ll> parent(n,-1);
    ll start=-1;
    ll end=-1;
    for(ll i=0;i<n;i++)
    {
        if(visited[i]==0 && dfs(i,G,parent,visited,start,end))
        {
            break;
        }
    }
    if(start==-1 || end==-1)
    {
        cout<<-1;
        return 0;
    }
    cout<<0<<endl;
    return 0;
    vector<ll> ans;
    set<ll> s;
    ans.push_back(start);
    for (ll v = end; v != start; v = parent[v])
        ans.push_back(v);
    reverse(ans.begin(),ans.end());
//    ContainerError(ans);
    clean(G,s,n);
    solve(ans,G,s,n,ans.size());
//    if(ans.size()<=1)
//    {
//        cout<<0<<endl;
//        return 0;
//    }
    cout<<ans.size()<<endl;
    for(auto &x:ans)
    {
        cout<<x+1<<endl;
    }
    return 0;
}































Measured memory_bytes: 384