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.42.0) (judge verdict: Accepted)

// https://atcoder.jp/contests/abc142/tasks/abc142_f
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

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

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

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

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

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

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

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

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

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

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

type DirectedGraph = Vec<Vec<usize>>;

struct StronglyConnectedComponent {
    n: usize,
    graph: DirectedGraph,
    reversed_graph: DirectedGraph,
    component_ids: Vec<usize>
}

impl StronglyConnectedComponent {
    fn new(g: DirectedGraph) -> Self {
        let n = g.len();
        let mut rg = vec![vec![]; n];
        for i in 0..n {
            for &to in &g[i] {
                rg[to].push(i);
            }
        }
        Self {
            n: g.len(),
            graph: g,
            reversed_graph: rg,
            component_ids: vec![]
        }
    }

    fn add_edge(&mut self, i: usize, j: usize) {
        self.graph[i].push(j);
        self.reversed_graph[j].push(i);
    }

    fn decompose(&mut self) -> usize {
        let mut visited = vec![false; self.n];
        let mut rev_order = vec![];
        for i in 0..self.n {
            if !visited[i] {
                self.dfs(i, &mut visited, &mut rev_order);
            }
        }

        let mut visited = vec![false; self.n];
        let mut id = 0;
        let mut component_ids = vec![0; self.n];
        for &r in rev_order.iter().rev() {
            if !visited[r] {
                self.rdfs(r, id, &mut visited, &mut component_ids);
                id += 1;
            }
        }
        self.component_ids = component_ids;
        id
    }

    fn dfs(&self, now: usize, visited: &mut Vec<bool>, rev_order: &mut Vec<usize>) {
        visited[now] = true;
        for &to in &self.graph[now] {
            if !visited[to] {
                self.dfs(to, visited, rev_order);
            }
        }
        rev_order.push(now);
    }

    fn rdfs(&self, now: usize, id: usize, visited: &mut Vec<bool>, component_ids: &mut Vec<usize>) {
        visited[now] = true;
        component_ids[now] = id;
        for &to in &self.reversed_graph[now] {
            if !visited[to] {
                self.rdfs(to, id, visited, component_ids);
            }
        }
    }
}

//===


fn check(g: &Vec<Vec<usize>>, vs: &Vec<usize>) -> bool {
    let n = g.len();
    let mut has = vec![0; n];
    for &v in vs {
        has[v] = 1;
    }
    for &v in vs {
        let mut cnt = 0;
        for &to in &g[v] {
            cnt += has[to];
        }
        if cnt != 1 {
            return false;
        }
    }
    true
}

fn find(g: &Vec<Vec<usize>>, root: usize, first: usize) -> Option<Vec<usize>> {
    let n = g.len();
    let mut dp = vec![n; n];
    dp[root] = 0;
    dp[first] = 1;
    for &to in &g[root] {
        if to != first {
            dp[to] = 0;
        }
    }
    let mut came_from = vec![0; n];
    came_from[first] = root;

    let mut deq = VecDeque::new();
    deq.push_back(first);

    // let mut best = None;
    loop {
        if deq.len() == 0 {
            return None;
        }
        let mut next_deq = VecDeque::new();
        while let Some(v) = deq.pop_front() {
            for &to in &g[v] {
                if to == root || dp[to] < dp[v] {
                    let mut vs = vec![to];
                    let mut now = v;
                    while now != to && now != root {
                        vs.push(now);
                        now = came_from[now];
                    }
                    if check(g, &vs) {
                        return Some(vs);
                    }
                    continue;
                }
                if dp[to] <= dp[v] {
                    continue;
                }
                dp[to] = dp[v] + 1;
                came_from[to] = v;
                next_deq.push_back(to);
            }
        }
        deq = next_deq;
    }
}

fn main() {
    input! {
        n: usize, m: usize,
        edges: [(usize1, usize1); m]
    };

    let mut g = vec![vec![]; n];
    for &(u, v) in &edges {
        g[u].push(v);
    }

    for i in 0..n {
        for &to in &g[i] {
            if let Some(v) = find(&g, i, to) {
                println!("{}", v.len());
                for v in v {
                    println!("{}", v+1);
                }
                return;
            }
        }
    }
    println!("-1");
}