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 LuaJIT (2.0.4) (judge verdict: Wrong Answer)

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  for i = 1, n do self.stage[stagenum][i] = i end
  for i = n + 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local stagenum = self.stagenum
  local ret = self.emptyvalue
  while left <= right do
    local stage, sz = 1, bls(1, stagenum - 1)
    local len = right - left + 1
    while (left - 1) % sz ~= 0 or len < sz do
      stage, sz = stage + 1, brs(sz, 1)
    end
    ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.update = function(self, idx)
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.top = function(self) return self.stage[1][1] end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n")
local longest_len = {}
local edge = {}
local invedge = {}
local updated = {}
local startcand = {}
for i = 1, n + 1 do
  longest_len[i] = -1
  edge[i] = {}
  invedge[i] = {}
  updated[i] = false
  startcand[i] = true
end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  edge[a][b] = true
  invedge[b][a] = true
  startcand[b] = false
end
local function getmax(a, b)
  if updated[a] and updated[b] then
    return longest_len[a] < longest_len[b] and b or a
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end
-- find loop candidate
local st = SegTree.new(n, getmax, n + 1)
for i = 1, n do
  longest_len[i] = 0
  updated[i] = true
end
st:updateAll()
while true do
  local src = st:top()
  if src == n + 1 then break end
  for dst, _u in pairs(edge[src]) do
    if longest_len[dst] < 0 or longest_len[dst] < longest_len[src] + 1 then
      if longest_len[dst] < n then
        longest_len[dst] = longest_len[src] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
  end
  updated[src] = false
  st:update(src)
end

local loopCandidates = {}
for i = 1, n do
  if n <= longest_len[i] then
    loopCandidates[i] = true
  end
end
-- remove no-loop points from loop candidate points
while true do
  local removed = false
  for src, _u in pairs(loopCandidates) do
    local f = false
    for dst, _u2 in pairs(edge[src]) do
      if loopCandidates[dst] then
        f = true
        break
      end
    end
    if not f then loopCandidates[src], removed = nil, true break end
  end
  if not removed then break end
end

-- remove all no-loop edges
for src, _u in pairs(loopCandidates) do
  for dst, _u2 in pairs(edge[src]) do
    if not loopCandidates[dst] then edge[src][dst] = nil end
  end
  for dst, _u2 in pairs(invedge[src]) do
    if not loopCandidates[dst] then invedge[src][dst] = nil end
  end
end
for i = 1, n do
  if not loopCandidates[i] then
    edge[i] = {}
    invedge[i] = {}
  end
end

-- find first length and second length from certain point
local loop_len = {}
local loop_started = {}
for src, _u in pairs(loopCandidates) do
  loop_len[src] = {-1, -1}
  loop_started[src] = false
end
if next(loopCandidates) then
  local src = next(loopCandidates)
  loop_len[src][1] = 0
  updated[src] = true
end

local function getlooptask(a, b)
  if updated[a] and updated[b] then
    if loop_len[a][2] < 0 and loop_len[b][2] < 0 then
      return loop_len[a][1] < loop_len[b][1] and a or b
    elseif loop_len[a][2] < 0 then return a
    elseif loop_len[b][2] < 0 then return b
    else
      return loop_len[a][2] < loop_len[b][2] and a or b
    end
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end

st.func = getlooptask
st:updateAll()

while true do
  local src = st:top()
  if src == n + 1 then break end
  for dst, _u in pairs(edge[src]) do
    if loop_started[dst] then
      if loop_started[src] then
        if loop_len[dst][2] < 0 or loop_len[src][2] + 1 < loop_len[dst][2] then
          loop_len[dst][2] = loop_len[src][2] + 1
          updated[dst] = true
          st:update(dst)
        end
      else
        if loop_len[dst][2] < 0 or loop_len[src][1] + 1 < loop_len[dst][2] then
          loop_len[dst][2] = loop_len[src][1] + 1
          updated[dst] = true
          st:update(dst)
        end
      end
    else
      if loop_len[dst][1] < 0 or loop_len[src][1] + 1 < loop_len[dst][1] then
        loop_len[dst][1] = loop_len[src][1] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
  end
  loop_started[src] = true
  updated[src] = false
  st:update(src)
end

-- get start of shortest loop
local short_loop_start = false
local short_loop_len = false
local short_loop_v1 = false
local short_loop_v2 = false
for src, v in pairs(loop_len) do
  local len = v[2] - v[1]
  if 0 < len then
    local f = false
    if not short_loop_len or len < short_loop_len then
      f = true
    elseif short_loop_len == len then
      if v[1] < short_loop_v1 then
        f = true
      end
    end
    if f then
      short_loop_start, short_loop_len = src, len
      short_loop_v1, short_loop_v2 = v[1], v[2]
    end
  end
end

-- recalculate length
local shortest_len = {}
for i = 1, n do
  shortest_len[i] = -1
  updated[i] = false
end
local function getmin(a, b)
  if updated[a] and updated[b] then
    return shortest_len[a] < shortest_len[b] and a or b
  elseif updated[a] then return a
  elseif updated[b] then return b
  else return n + 1
  end
end

if short_loop_start then
  shortest_len[short_loop_start] = 0
  updated[short_loop_start] = true
  st.func = getmin
  st:updateAll()
  while true do
    local src = st:top()
    if src == n + 1 then break end
    for dst, _u in pairs(edge[src]) do
      if shortest_len[dst] < 0 or shortest_len[src] + 1 < shortest_len[dst] then
        shortest_len[dst] = shortest_len[src] + 1
        updated[dst] = true
        st:update(dst)
      end
    end
    updated[src] = false
    st:update(src)
  end
  -- restore path
  local ans = {short_loop_start}
  for i = short_loop_len - 1, 1, -1 do
    local dst = ans[#ans]
    for src, _u in pairs(invedge[dst]) do
      if shortest_len[src] == i then
        table.insert(ans, src)
        break
      end
    end
  end
  print(short_loop_len)
  print(table.concat(ans, "\n"))
else
  print(-1)
end