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 Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*);
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};
#[allow(unused_imports)]
use std::io::{stdout, stdin, BufWriter, Write};

// pub struct Graph {
//     n: usize,
//     edges: Vec<Vec<usize>>,
// }
// impl Graph {
//     pub fn dfs(&self, cur: usize, from: usize, root: usize, seen: &mut Vec<bool>) -> bool {
//         if seen[cur] {
//             return true;
//         }

//         seen[cur] = true;
//         for &to in self.edges[cur].iter() {
//             if to != from {

//                 if self.dfs(to, cur) {
//                     return true;
//                 }
//             }
//         }
//         false;
//     }
// }

use std::collections::VecDeque;

pub fn decompose(graph: &Vec<Vec<usize>>) -> Vec<usize> {
    let mut vs = Vec::new();
    let num_v = graph.len();
    let mut cmp = vec![0; num_v];

    let mut reverse_graph = vec![vec![]; num_v];
    for i in 0..num_v {
        for &v in graph[i].iter() {
            reverse_graph[v].push(i);
        }
    }
    let mut used = vec![false; num_v];

    let mut stack = VecDeque::new();
    let mut added = vec![false; num_v];
    for i in 0..num_v {
        if used[i] {
            continue;
        }
        stack.push_front(i);
        while let Some(v) = stack.pop_front() {
            stack.push_front(v);
            used[v] = true;
            let mut pushed = false;
            for &u in graph[v].iter().rev() {
                if !used[u] {
                    stack.push_front(u);
                    pushed = true;
                }
            }
            if !pushed {
                stack.pop_front();
                if !added[v] {
                    vs.push(v);
                    added[v] = true;
                }
            }
        }
    }

    used = vec![false; num_v];
    let mut k = 0;
    vs.reverse();
    for &i in vs.iter() {
        if used[i] {
            continue;
        }
        stack.push_front(i);
        used[i] = true;
        cmp[i] = k;
        while let Some(v) = stack.pop_front() {
            stack.push_front(v);
            let mut pushed = false;
            for &u in reverse_graph[v].iter() {
                if used[u] {
                    continue;
                }
                used[u] = true;
                cmp[u] = k;
                stack.push_front(u);
                pushed = true;
            }
            if !pushed {
                stack.pop_front();
            }
        }
        k += 1;
    }

    return cmp;
}

#[derive(Debug)]
pub struct Graph {
    n: usize,
    edges: Vec<Vec<usize>>,
}
impl Graph {
    pub fn bfs(&self, s: usize) -> Vec<i64> {
        use std::collections::VecDeque;
        let mut q = VecDeque::new();
        let mut dist = vec![-1; self.n];
        dist[s] = 0;
        q.push_back(s);
        while let Some(cur) = q.pop_front() {
            for &adj in self.edges[cur].iter() {
                if dist[adj] == -1 {
                    dist[adj] = dist[cur] + 1;
                    q.push_back(adj);
                }
            }
        }
        dist
    }
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }

    input!{
      n: usize,
      m: usize,
      abs: [(usize1, usize1); m],
    }
    let mut e = vec![vec![]; n];
    for (a, b) in abs {
        e[a].push(b);
    }

    let components = decompose(&e);
    let mc = *components.iter().max().unwrap();
    let mut cs = vec![vec![]; mc+1];
    for i in 0..n {
        let c = components[i];
        cs[c].push(i);
    }

    for i in 0..cs.len() {
        let nodes = &cs[i];
        if nodes.len() == 1 {
            continue;
        }

        use std::collections::HashSet;
        let mut set = HashSet::new();
        for &n in nodes.iter() {
            set.insert(n);
        }

        let mut e2 = vec![vec![]; n];
        for &nd in nodes {
            for &t in e[nd].iter() {
                if set.contains(&t) {
                    e2[nd].push(t);
                }
            }
        }


        let mut ans = vec![];
        use std::collections::VecDeque;
        let mut q = VecDeque::new();
        let mut dist = vec![-1; n];
        let s = nodes[0];
        dist[s] = 0;
        q.push_back(s);
        while let Some(cur) = q.pop_front() {
            ans.push(cur);
            for &adj in e2[cur].iter() {
                if dist[adj] == -1 {
                    dist[adj] = dist[cur] + 1;
                    q.push_back(adj);
                }
            }
        }

        puts!("{}\n", ans.len());
        for a in ans {
            puts!("{}\n", a+1);
        }
        return;
    }
    puts!("{}\n", -1);
}