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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

// http://kmjp.hatenablog.jp/entry/2019/09/28/1030

import java.util.*;

public class Main {
  static Map<Integer, List<Integer>> edgeE = new HashMap<>();
  static List<Integer> vStack = new ArrayList<>(); // stack in DFS
  static int[] vStackState; // state of DFS.
  static List<Integer> cycle = new ArrayList<>();
  static Integer[] cycleSeqId; // id of cycle member.

  // Find cycle
  static void dfs(int cur) {
    if (vStackState[cur] == 2) { // Found node visited in current dfs. (a cycle)
      if (cycle.isEmpty()) {
        for (int i = 0; i < vStack.size(); i++) {
          if (vStack.get(i) == cur) { // Find starting point to avoid circle shape of `6`.
            for (int j = i; j < vStack.size(); j++) {
              cycle.add(vStack.get(j));
              cycleSeqId[vStack.get(j)] = j - i;
            }
            break;
          }
        }
      }
      return;
    }
    if (vStackState[cur] == 1) return;
    vStackState[cur] = 2;
    vStack.add(cur);
    for (Integer e : edgeE.get(cur)) {
      dfs(e);
    }
    vStack.remove(vStack.size() - 1);
    vStackState[cur] = 1;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    for (int i = 1; i <= N; i++) {
      edgeE.put(i, new ArrayList<>());
    }
    int M = sc.nextInt();
    for (int i = 0; i < M; i++) {
      int a = sc.nextInt();
      int b = sc.nextInt();
      edgeE.get(a).add(b);
    }
    cycleSeqId = new Integer[N + 1];
    vStackState = new int[N + 1]; // 1-indexed (skip 0-th)
    Arrays.fill(cycleSeqId, -1);
    /* Search cycle. */
    for (int i = 1; i <= N; i++) { // if cycle is already found, loop is useless?
//  NG:    for (int i = 1; i <= N && !cycle.isEmpty(); i++) { // if cycle is already found, loop is useless?
      Arrays.fill(vStackState, 0);
      vStack.clear();
      dfs(i);
    }
    if (cycle.isEmpty()) {
      System.out.println("-1");
      return;
    }
    retry:
    for (int idxStart = 0; idxStart < cycle.size(); idxStart++) {
      Integer nodeIdxFrom = cycle.get(idxStart);
      for (Integer to : edgeE.get(nodeIdxFrom)) {
        if (
        /*member of cycle */ cycleSeqId[to] != -1
            && /* If path split. This is smaller cycle. */
          cycleSeqId[to] != (cycleSeqId[nodeIdxFrom] + 1) % cycle.size()) {
          List<Integer> C2 = new ArrayList<>();
          int newCycleSeqStartIdx = cycleSeqId[to]; // New starting point to avoid circle shape of `6`.
          Arrays.fill(cycleSeqId, -1);
          for (int j = 0; j < cycle.size(); j++) {
            Integer nodeIdx = cycle.get((newCycleSeqStartIdx + j) % cycle.size());
            C2.add(nodeIdx);
            cycleSeqId[nodeIdx] = j;
            if (nodeIdx.equals(nodeIdxFrom)) break;
          }
          cycle = C2;
          idxStart = -1;
          continue retry;
        }
      }
    }
    System.out.println(cycle.size());
    for (Integer c : cycle) {
      System.out.println(c);
    }
  }
}