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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;

using SyrupBeans.Competitive.Collections;
using SyrupBeans.Competitive.Graph;

using E = System.Linq.Enumerable;

public class Program
{
    public static void Main()
    {
        var nm = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
        var n = nm[0];
        var m = nm[1];
        var adjList = new List<int>[n + 1];
        for (int i = 0; i < adjList.Length; ++i)
        {
            adjList[i] = new List<int>();
        }
        for (int i = 0; i < m; ++i)
        {
            var ab = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
            adjList[ab[0]].Add(ab[1]);
        }

        var minimalCycle = new PersistentStack<int>();
        var cycleLength = int.MaxValue;
        var walk = new AdjacencyListWalk(adjList, 0);
        var visited = new bool[adjList.Length];
        var onWalk = new int[adjList.Length];
        var outdegree = adjList.Select(x => x.Count).ToArray();
        // visited[0] = true;
        while (true)
        {
            int v = walk.End;
            if (onWalk[v] < 0 || outdegree[v] < 1)
            {
                onWalk[v] = -1;
                if (walk.IsLonger)
                {
                    walk.Shorten();
                    --outdegree[walk.End];
                }
            }
            else if (onWalk[v] == 0)
            {
                if (visited[v])
                {
                    walk.Shorten();
                    continue;
                }
                visited[v] = true;
                onWalk[v] = walk.Length;
            }
            else if (onWalk[v] < walk.Length)
            {
                var t = walk.Length - onWalk[v];
                walk.Shorten();
                if (t < cycleLength)
                {
                    cycleLength = t;
                    walk.ExtractAs(minimalCycle);
                }
            }

            while (walk.IsLonger && !walk.HasBranchToExtend)
            {
                onWalk[walk.End] = 0;
                walk.Shorten();
            }
            if (walk.HasBranchToExtend)
            {
                walk.Extend();
                continue;
            }
            if (cycleLength < int.MaxValue)
            {
                Console.WriteLine(cycleLength);
                Console.WriteLine(string.Join(Environment.NewLine, minimalCycle.Take(cycleLength)));
                return;
            }
            for (int u = walk.Start + 1; u < visited.Length; ++u)
            {
                if (visited[u])
                {
                    continue;
                }
                walk.StartOn(u);
                break;
            }
            if (walk.Start == v)
            {
                break;
            }
        }

        Console.WriteLine(-1);
    }
}

namespace SyrupBeans.Competitive.Collections
{
    public class PersistentStack<T> : IEnumerable<T>
    {
        class LinkedContainer
        {
            public readonly T Value = default(T);
            public readonly LinkedContainer Parent = default(LinkedContainer);
            public bool IsRoot => this.Parent == this;

            private LinkedContainer()
            {
                this.Parent = this;
            }

            public LinkedContainer(LinkedContainer parent, T value)
            {
                this.Value = value;
                this.Parent = parent;
            }

            public static LinkedContainer Root { get; } = new LinkedContainer();
        }

        private LinkedContainer linkedContainer { get; set; } = default(LinkedContainer);
        public int Count { get; private set; } = -1;

        public PersistentStack()
        {
            this.linkedContainer = LinkedContainer.Root;
            this.Count = 0;
        }

        public PersistentStack(IEnumerable<T> collection)
        : this()
        {
            foreach (var x in collection)
            {
                Push(x);
            }
        }

        public void Push(T item)
        {
            this.linkedContainer = new LinkedContainer(this.linkedContainer, item);
            ++this.Count;
        }

        public T Peek()
        {
            if (this.linkedContainer.IsRoot)
            {
                throw new InvalidOperationException("PersistenceStack empty.");
            }

            return this.linkedContainer.Value;
        }

        public T Pop()
        {
            var x = Peek();
            --Count;
            this.linkedContainer = this.linkedContainer.Parent;
            return x;
        }

        public void Clear()
        {
            this.Count = 0;
            while (!this.linkedContainer.IsRoot)
            {
                this.linkedContainer = this.linkedContainer.Parent;
            }
        }

        public void Share(PersistentStack<T> source)
        {
            this.linkedContainer = source.linkedContainer;
            this.Count = source.Count;
        }

        public IEnumerator<T> GetEnumerator()
        {
            for (var x = this.linkedContainer; !x.IsRoot; x = x.Parent)
            {
                yield return x.Value;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => this.GetEnumerator();
    }
}

namespace SyrupBeans.Competitive.Graph
{
    public class AdjacencyListWalk
    {
        public List<int>[] Graph { get; private set; } = null;
        public int Start { get; private set; } = -1;
        public int End => walk.Peek();

        private Stack<int> branchHistory = null;
        private PersistentStack<int> walk = null;
        public int Length => walk.Count;

        public AdjacencyListWalk(List<int>[] adjacencyList, int start = 0)
        {
            this.Graph = adjacencyList;
            this.branchHistory = new Stack<int>();
            this.walk = new PersistentStack<int>();

            StartOn(start);
        }

        public void ExtractAs(PersistentStack<int> obtainer) => obtainer.Share(this.walk);

        public void StartOn(int v)
        {
            this.branchHistory.Clear();
            this.walk.Clear();

            this.branchHistory.Push(-1);
            this.walk.Push(v);
            this.Start = v;
        }

        public bool HasBranchToExtend => this.branchHistory.Peek() + 1 < this.Graph[End].Count;

        public bool Extend()
        {
            if (!this.HasBranchToExtend)
            {
                return false;
            }

            int order = this.branchHistory.Pop() + 1;
            this.branchHistory.Push(order);
            this.branchHistory.Push(-1);
            this.walk.Push(this.Graph[this.End][order]);
            return this.HasBranchToExtend;
        }

        public bool IsLonger => this.walk.Count > 1;

        public bool Shorten()
        {
            if (!this.IsLonger)
            {
                return false;
            }

            this.branchHistory.Pop();
            this.walk.Pop();
            return this.IsLonger;
        }
    }
}