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: Wrong Answer)

/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << endl;
    err(++it, args...);
}

const ll MODBASE = 1000000007LL;
const int MAXN = 1010;
const int MAXM = 1010;
const int MAXK = 110;
const int MAXQ = 200010;

int n, m, low[MAXN], num[MAXN], dfsNum, sccCnt, w[MAXN];
vector<II> edges;
vector<int> a[MAXN], cmp[MAXN];
bool visited[MAXN];
stack<int> s;
set<int> se;

void scc(int u) {
    low[u] = num[u] = ++dfsNum;
    s.push(u);
    visited[u] = true;
    FOR(i,0,SZ(a[u])-1) {
        int v = a[u][i];
        if (num[v] == -1) scc(v);
        if (visited[v]) low[u] = min(low[u], low[v]);
    }
    if (low[u] == num[u]) {
        int k = -1;
        sccCnt++;
        int dem = 0;
        do {
            k = s.top();
            cmp[sccCnt].emplace_back(k);
            s.pop();
            w[k] = sccCnt;
            visited[k] = false;
            dem++;
        } while (k != u);
    }
}

bool found = false, inside = true;
int root = -1;
vector<int> b, c;

void dfs(int u) {
    visited[u] = true;
    FOR(i,0,SZ(a[u])-1) {
        int v = a[u][i];
        if (!visited[v]) {
            dfs(v);
            if (found) {
                if (inside) b.emplace_back(u);
                if (u == root) inside = false;
                return;
            }
        }
        else {
            found = true;
            root = v;
            b.emplace_back(u);
        }
    }
}

void solve(int compIndex) {
    FOR(i,1,n) a[i].clear();

    FOR(i,0,SZ(cmp[compIndex])-1) {
        int u = cmp[compIndex][i];
        se.insert(u);
        visited[u] = false;
    }

    FOR(i,0,SZ(edges)-1) 
        if (se.count(edges[i].FI) && se.count(edges[i].SE)) a[edges[i].FI].emplace_back(edges[i].SE);
    
    dfs(cmp[compIndex][0]);
    reverse(ALL(b));

    while (SZ(edges) > 0) {
        // FOR(i,0,SZ(b)-1) cout << b[i] << ' ';
        // cout << endl;
        se.clear();
        FOR(i,0,SZ(b)-1) {
            int u = b[i];
            se.insert(u);
        }

        while (SZ(edges) > 0) {
            II r = edges.back();
            edges.pop_back();
            if (!(se.count(r.FI) && se.count(r.SE))) continue;

            int u = r.FI, v = r.SE;
            int posU = -1, posV = -1;

            FOR(i,0,SZ(b)-1) {
                if (b[i] == u) posU = i;
                if (b[i] == v) posV = i;
            }

            // cout << endl;
            // cout << u << ' ' << v << endl;
            // cout << posU << ' ' << posV << endl;

            if ((posU + 1) % SZ(b) == posV) continue;

            c.clear();
            while (posV != posU) {
                c.emplace_back(b[posV]);
                posV = (posV + 1) % SZ(b);
            }
            c.emplace_back(b[posU]);

            b = c;
            break;
        }
    }

    cout << SZ(b) << "\n";
    FOR(i,0,SZ(b)-1) cout << b[i] << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> m;
    FOR(i,1,m) {
        int u, v;
        cin >> u >> v;
        edges.push_back(II(u, v));
        a[u].emplace_back(v);
    }
    FOR(i,1,n) num[i] = -1;
    FOR(i,1,n)
        if (num[i] == -1) scc(i);
    FOR(i,1,sccCnt) {
        if (SZ(cmp[i]) != 1) {
            solve(i);
            return 0;
        }
    }
    cout << -1;
    return 0;
}