#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}


macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;
use std::collections::BinaryHeap;

fn min_cycle(s: usize, g: &Vec<Vec<usize>>) -> Option<Vec<usize>> {
    let n = g.len();
    let mut q = BinaryHeap::new();
    let mut d: Vec<i64> = vec![n as i64 + 10; n];
    let mut rev: Vec<usize> = vec![n; n];
    for &v in g[s].iter() {
        q.push((0, v));
        d[v] = 0;
        rev[v] = v;
    }
    while let Some((f, u)) = q.pop() {
        if -f > n as i64 {
            continue
        }
        if u == s {
            let mut v = u;
            let mut r = vec![v];
            while rev[v] != v {
                v = rev[v];
                r.push(v);
            }
            return Some(r)
        }
        for &v in g[u].iter() {
            if d[v] > d[u] + 1 {
                d[v] = d[u] + 1;
                rev[v] = u;
                q.push((-d[v], v));
            }
        }
    }
    None
}

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let m: usize = sc.cin();
    let mut g = vec![vec![]; n];
    for _ in 0..m {
        let u: usize = sc.cin();
        let v: usize = sc.cin();
        g[u - 1].push(v - 1);
    }
    let mut ans: Option<Vec<usize>> = None;
    let mut minlen = n + m + 10;
    for u in 0..n {
        if let Some(c) = min_cycle(u, &g) {
            if ans.is_none() || minlen > c.len() {
                minlen = c.len();
                ans = Some(c);
            }
        }
    }

    if let Some(c) = ans {
        put!(c.len());
        for x in c.iter() { put!(x + 1); }
    } else {
        put!(-1);
    }
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}


Measured memory_bytes: 4352