module DirectedGraph (VSet : Set.S) :
sig
  (* トポロジカルソート *)
  val sort :
    (* 頂点のリスト *)
    VSet.elt list ->
    (* 隣接リスト *)
    (VSet.elt -> VSet.elt list) ->
    VSet.elt list

  (* 強連結成分分解 *)
  val scc :
    (* 頂点のリスト *)
    VSet.elt list ->
    (* 隣接リスト *)
    (VSet.elt -> VSet.elt list) ->
    VSet.elt list list
end =
struct
  let rec visit es v (vs, l) =
    if VSet.mem v vs then
      let (vs', l') =
        List.fold_right (visit es) (es v) (VSet.remove v vs, l) in
      (vs', v :: l')
    else (vs, l)

  let sort vs es =
    snd @@ List.fold_right (visit es) vs (VSet.of_list vs, [])

  let scc vs es =
    snd @@ List.fold_right (fun v (vs, l) ->
      if VSet.mem v vs then
        let (vs', cs) = visit es v (vs, []) in
        (vs', cs :: l)
      else (vs, l)) (sort vs es) (VSet.of_list vs, [])
end

module G = DirectedGraph (Set.Make (struct
  type t = int
  let compare = compare
end))

exception Answer of int list

let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let es = Array.make (n + 1) [] in
  for i = 0 to m - 1 do
    Scanf.scanf "%d %d\n" @@ fun a b ->
      es.(a) <- b :: es.(a)
  done;
  try
    let v :: _ =
      List.find (function
        | [] | [_] -> false
        | _ :: _ :: _ -> true) @@
      G.scc (Array.to_list @@ Array.init n (( + ) 1)) (Array.get es) in
    let visited = Array.make (n + 1) false in
    let rec dfs vs v =
      if visited.(v)
      then raise (Answer vs)
      else begin
        visited.(v) <- true;
        List.iter (fun u -> dfs (v :: vs) u) es.(v);
        visited.(v) <- false
      end in
    let vs = try dfs [] v; [] with Answer vs -> vs in
    Printf.printf "%d\n" (List.length vs);
    List.iter (Printf.printf "%d\n") vs
  with Not_found -> print_endline "-1"

