Problem:
Score : 600 points

Problem Statement

Given is a directed graph G with N vertices and M edges.

The vertices are numbered 1 to N, and the i-th edge is directed from Vertex A_i to Vertex B_i.

It is guaranteed that the graph contains no self-loops or multiple edges.

Determine whether there exists an induced subgraph (see Notes) of G such that the in-degree and out-degree of every vertex are both 1. If the answer is yes, show one such subgraph.

Here the null graph is not considered as a subgraph.

Notes

For a directed graph G = (V, E), we call a directed graph G' = (V', E') satisfying the following conditions an induced subgraph of G:

V' is a (non-empty) subset of V.

E' is the set of all the edges in E that have both endpoints in V'.

Constraints

1 \leq N \leq 1000

0 \leq M \leq 2000

1 \leq A_i,B_i \leq N

A_i \neq B_i

All pairs (A_i, B_i) are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

If there is no induced subgraph of G that satisfies the condition, print -1.
Otherwise, print an induced subgraph of G that satisfies the condition, in the following format:

K
v_1
v_2
:
v_K

This represents the induced subgraph of G with K vertices whose vertex set is \{v_1, v_2, \ldots, v_K\}. (The order of v_1, v_2, \ldots, v_K does not matter.)
If there are multiple subgraphs of G that satisfy the condition, printing any of them is accepted.

Sample Input 1

4 5
1 2
2 3
2 4
4 1
4 3

Sample Output 1

3
1
2
4

The induced subgraph of G whose vertex set is \{1, 2, 4\} has the edge set \{(1, 2), (2, 4), (4, 1)\}. The in-degree and out-degree of every vertex in this graph are both 1.

Sample Input 2

4 5
1 2
2 3
2 4
1 4
4 3

Sample Output 2

-1

There is no induced subgraph of G that satisfies the condition.

Sample Input 3

6 9
1 2
2 3
3 4
4 5
5 6
5 1
5 2
6 1
6 2

Sample Output 3

4
2
3
4
5

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:https://atcoder.jp/contests/abc142/tasks/abc142_f>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int N,M;
vector<int> G[1000];
bool used_v[1000];
set<int> state,next_state;
set<pii> used_edge;
int in[1000],out[1000];

bool check(){
    fill(in,in+1000,0);
    fill(out,out+1000,0);
    for(auto n:state){
        for(auto next:G[n]){
            if(state.find(next) != state.end()){
                out[n]++;
                in[next]++;
            }
        }
    }
    for(auto n:state){
        if(in[n] == 1 && out[n] == 1) continue;
        return false;
    }
    return true;
}

bool detect_cycle(int n){
    if(used_v[n]) return true;
    used_v[n] = true;
    for(auto next:G[n]){
        if(state.find(next) != state.end()){
            if(detect_cycle(next)){
                used_edge.insert(pii(n,next));
                return true;
            }
        }
    }
    used_v[n] = 0;
    return false;
}
bool detect_loop(int n,int t){
    if(n == t){
        used_v[n] = true;
        return true;
    }
    for(auto next:G[n]){
        if(state.find(next) != state.end()){
            if(detect_loop(next,t)){
                used_v[n] = true;
                return true;
            }
        }
    }
    return false;
}

bool first_update(){
    for(auto n:state){
        used_edge.clear();
        fill(used_v,used_v+1000,false);
        if(detect_cycle(n)) break;
    }
    if(used_edge.empty()) return false;
    
    for(int i = 0; i < N;i++){
        if(used_v[i]) next_state.insert(i);
    }
    state = next_state;
    return true;
}
template<class Type>
Type solve(Type res = Type()){
    // Input
    cin >> N >> M;
    for(int i = 0; i < M;i++){
        int A,B; cin >> A >> B;
        A--; B--;
        G[A].push_back(B);
    }
    for(int i = 0; i < N;i++) state.insert(i);
    
    // Main
    if(!first_update()){
        cout << -1 << endl;
        return res;
    }
    while(!check()){
        // for(auto n:state) cout << n << " "; cout << endl;

        for(auto n:state){
            used_edge.clear();
            fill(used_v,used_v+1000,false);
            if(detect_cycle(n)) break;
        }
        
        [&](){
            for(auto n:state){
                if(state.find(n) == state.end()) continue;
                for(auto next:G[n]){
                    if(state.find(next) == state.end()) continue;
                    if(used_edge.find(pii(n,next)) != used_edge.end()) continue;
                    
                    fill(used_v,used_v+1000,false);
                    detect_loop(next,n);
                
                    
                    next_state.clear();
                    for(auto cand:state){
                        if(used_v[cand]){
                            next_state.insert(cand);
                        }
                    }
                    state = next_state;
                    return;
                }
            }
            return;
        }();
    }
    
    // Output
    cout << state.size() << endl;
    for(auto n:state){
        cout << n+1 << endl;
    }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    solve(0);
    // cout << fixed << setprecision(15) << solve<ll>() << endl;
    return 0;
}