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: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...) cerr << "line " << __LINE__ << " : "; debug_print_in(x);

template <typename First>
void debug_print_in(First first){
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest){
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

template<class t, class u> void chmax(t&a,u b){if(a<b)a=b;};
template<class t, class u> void chmin(t&a,u b){if(a>b)a=b;};
int popcount(int t){return __builtin_popcount(t);} //GCC
int popcount(ll t){return __builtin_popcountll(t);} //GCC

template <typename T>
void vec_print(vector<T> VEC){
  REP(i, VEC.size()){
    cerr << VEC[i] << " ";
  }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T> > MAT){
  REP(i,MAT.size()){
    REP(j,MAT[i].size()){
      cerr << MAT[i][j] << " ";
    }
    cerr << endl;
  }
};

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  set<pair<ll, ll>> st3;

  ll N, M;
  cin >> N >> M;
  vll OUT(N,0), IN(N,0);
  vvll Graph(N), InvGraph(N);
  REP(i,M){
    ll A, B;
    cin >> A >> B;
    A--; B--;
    Graph[A].pb(B);
    InvGraph[B].pb(A);
    OUT[A]++; IN[B]++;
    st3.emplace(make_pair(A, B));
  }

  /*
  REP(i,N){
    debug_print(i,IN[i],OUT[i]);
  }
  */

  REP(i,N){
    stack<ll> que;
    que.push(i);
    while(que.size()){
      ll pos = que.top(); que.pop();
      if(IN[pos]>=1) continue;
      REP(j,Graph[pos].size()){
        ll next_pos = Graph[pos][j];
        OUT[pos]--;
        IN[next_pos]--;
        que.push(next_pos);
      }
    }
  }

  REP(i,N){
    stack<ll> que;
    que.push(i);
    while(que.size()){
      ll pos = que.top(); que.pop();
      if(OUT[pos]>=1) continue;
      REP(j,InvGraph[pos].size()){
        ll next_pos = InvGraph[pos][j];
        IN[pos]--;
        OUT[next_pos]--;
        que.push(next_pos);
      }
    }
  }

  /*
  REP(i,N){
    debug_print(i,IN[i],OUT[i]);
  }
  */

  set<ll> st;

  REP(i,N){
    if(IN[i]<=0) continue;
    if(OUT[i]<=0) continue;
    st.emplace(i);
  }

  vll ans;
  ll final_val = -1;

  REP(i,N){
    if(st.find(i)==st.end()) continue;

    set<ll> st2;
    queue<ll> que;
    que.push(i);
    while(que.size()){
      ll pos = que.front(); que.pop();
      if(st2.find(pos)!=st2.end()){
        final_val = pos;
        break;
      }
      ans.push_back(pos);
      st2.emplace(pos);

      queue<ll> que2;
      ll next_pos = -1;
      REP(i,InvGraph[pos].size()){
        ll tmp = InvGraph[pos][i];
        if(st.find(tmp)==st.end()) continue;
        que2.push(tmp);
      }
      while(que2.size()){
        ll tmp = que2.front(); que2.pop();
        REP(i,InvGraph[tmp].size()){
          ll tmptmp = InvGraph[tmp][i];
          if(st.find(tmptmp)==st.end()) continue;
          if(tmptmp==pos){
            next_pos = tmp;
            goto HOGE;
          }
          que2.push(tmptmp);
        }
      }
HOGE:
      que.push(next_pos);
    }
    break;
  }

  ll target_index = 0;
  REP(i,ans.size()){
    if(ans[i]==final_val){
      target_index = i;
      break;
    }
  }

  if(ans.size()-target_index<=0){
    printf("-1\n");
    return 0;
  }
  printf("%lld\n", ans.size()-target_index);
  FOR(i,target_index,ans.size()){
    printf("%lld\n", ans[i]+1);
  }




  return 0;
}