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;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
#define IOS cin.tie(0),ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
template<class Type>struct is_vector : std::false_type {};
template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T> inline ostream& operator << (ostream& out, const vector<T>& v) {
	if (v.empty())return out;
	constexpr bool is_vector_v = is_vector<T>::value;
	if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
	else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
	return out;
}
inline void put() {}
template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
inline void putn() {}
template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

vector<int> cycle;

void dfs(vector<vector<int>>& path, int v, int p, stack<int>& st, vector<int>& vis) {
	if (cycle.size() > 0)return;
	/*while (!st.empty() && st.top() != p) {
		vis[st.top()] = 2;
		st.pop();
	}
	st.push(v);*/
	for (int i = 0; i < path[v].size(); ++i) {
		if (vis[path[v][i]] == 1) {
			if (cycle.size() == 0) {
				cycle.push_back(v);
				while (!st.empty() && st.top() != path[v][i]) {
					cycle.push_back(st.top());
					st.pop();
				}
				cycle.push_back(path[v][i]);
				reverse(cycle.begin(), cycle.end());
			}
			return;
		} else if (vis[path[v][i]] == 0) {
			vis[path[v][i]] = 1;
			st.push(v);
			dfs(path, path[v][i], v, st, vis);
			vis[path[v][i]] = 2;
			if(!st.empty())st.pop();
		}
	}
	return;
}

int main() {
	IOS;
	int n, m;
	cin >> n >> m;
	vector<int> a(m), b(m);
	vector<vector<int>> path(n);
	REP(i, m) {
		cin >> a[i] >> b[i];
		a[i]--;
		b[i]--;
		path[a[i]].push_back(b[i]);
	}
	stack<int> st;
	vector<int> vis(n, 0);
	vis[0] = 1;
	dfs(path, 0, -1, st, vis);
	if (cycle.size()==0) {
		put(-1);
		return 0;
	}
	vector<int> c(n, -1);
	int t = cycle.size();
	REP(i, cycle.size()) {
		c[cycle[i]] = i;
	}
	bool flag = true;
	while(flag) {
		flag = false;
		REP(j, cycle.size()) {
			int p = cycle[j];
			if (flag) {
				break;
			}
			REP(k, path[p].size()) {
				if (c[path[p][k]] >= 0&&c[path[p][k]]!=(j+1)%cycle.size()) {
					flag = true;
					int t1 = c[path[p][k]];
					fill(all(c), -1);
					if (t1 < j) {
						cycle.erase(cycle.begin() + p + 1, cycle.end());
						cycle.erase(cycle.begin(), cycle.begin() + t1);
					} else {
						cycle.erase(cycle.begin() + p + 1, cycle.begin() + t1);
					}
					REP(l, cycle.size()) {
						c[cycle[l]] = l;
					}
					break;
				}
			}
		}
	}
	put(cycle.size());
	REP(i, cycle.size()) {
  		put(cycle[i]+1);
	}
	return 0;
}