Language: C++

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#include <stdexcept>

using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;

//conversion
//------------------------------------------
inline int toint(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }

//debug
#define DUMP(x)  cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (l" << __line__ << ")" << " " << __file__ << endl

inline ll floor(ll a, ull b)
{
	ll llb = b;
	if (a >= 0)
	{
		return a / llb;
	}
	else
	{
		return - ( (-a + llb - 1) / llb);
	}
}

inline ll ceil(ll a, ull b)
{
	ll llb = b;
	if (a >= 0)
	{
		return (a + llb - 1) / llb;
	}
	else
	{
		return - (-a / llb);
	}
}

inline ll getLowestOneBit(ll a)
{
	return a & (-a);
}

inline ll getHighestOneBit(ll a)
{
	while ((a & (a - 1)) != 0)
	{
		a = a & (a - 1);
	}
	return a;
}

inline bool isPower2(ull a)
{
	if (a == 0)
	{
		return false;
	}
	return !(a & (a - 1));
}

inline ll getSpecifiedBit(ll a, unsigned bit)
{
	return (a & (1LL << bit)) >> bit;
}

// computational complexity: o(log(max(a, b))) 
inline ull getGcd(ull a, ull b)
{
	if (b == 0)
	{
		return a;
	}
	return getGcd(b, a % b);
}

template<class Integer>
inline Integer getPower(Integer base, ull exponential)
{
	Integer result = 1;
	while (exponential >= 1)
	{
		if (exponential & 1)
		{
			result = result * base;
		}
		base = base * base;
		exponential >>= 1;
	}

	return result;
}

vector<ll> z_algorithm(string S) {
	vector<ll> As(S.size());

	ll base_pos = 0;
	for (ll curr_pos = 1; curr_pos < S.size(); curr_pos++)
	{
		if (curr_pos + As[curr_pos - base_pos] < base_pos + As[base_pos])
		{
			As[curr_pos] = As[curr_pos - base_pos];
		}
		else
		{
			int check_pos = max(0LL, base_pos + As[base_pos] - curr_pos);
			while (curr_pos + check_pos < S.size() && S[check_pos] == S[curr_pos + check_pos]) ++check_pos;
			As[curr_pos] = check_pos;
			base_pos = curr_pos;
		}
	}
	As[0] = S.size();

	return As;
}

enum struct Status
{
	NOT_VISITED,
	ACTIVE,
	FINISHED
};

ll visit(ll i, const vector<vector<ll>>& edges, vector<Status>& node_status) {
	if (node_status[i] == Status::ACTIVE) return i;
	else if (node_status[i] == Status::NOT_VISITED)
	{
		ll node;
		node_status[i] = Status::ACTIVE;
		for (ll j = 0; j < edges[i].size(); ++j)
		{
			if ((node = visit(edges[i][j], edges, node_status)) != -1 ) return node;
		}
		node_status[i] = Status::FINISHED;
	}
	return -1;
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll N, M;
	cin >> N >> M;
	
	vector<vector<ll>> edges(N);
	ll a, b;
	for (ll i = 0; i < M ; ++i)
	{
		cin >> a >> b;
		--a, --b;
		edges[a].emplace_back(b);
	}

	vector<Status> node_status(N, Status::NOT_VISITED);
	ll node = 0;
	for (ll i = 0 ; i < N; ++i)
	{
		if (node_status[i] == Status::NOT_VISITED)
		{
			if ((node = visit(i, edges, node_status)) != -1 ) break;
		}
	}

	if (node == -1)
	{
		cout << -1 << endl;
		return 0;
	}

	ll begin = -1;
	vector<ll> nodes;
	while (true)
	{
		nodes.emplace_back(node);
		ll next_node = 0;
		begin = -1;
		for (ll j = 0; j < edges[node].size(); ++j)
		{
			if (find(nodes.begin(), nodes.end(), edges[node][j]) != nodes.end())
			{
				begin = max(begin, distance(nodes.begin(), find(nodes.begin(), nodes.end(), edges[node][j])));
			}
			if (node_status[edges[node][j]] == Status::ACTIVE) next_node = edges[node][j];
		}
		if (begin != -1) goto loop_end;
		node = next_node;
	}
loop_end:

	cout << nodes.size() - begin << endl;
	for (ll i = begin; i < nodes.size(); ++i)
	{
		cout << nodes[i] + 1 << endl;
	}

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:214:44: error: no matching function for call to 'max(ll&, std::__iterator_traits<__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >, void>::difference_type)'
  214 |                                 begin = max(begin, distance(nodes.begin(), find(nodes.begin(), nodes.end(), edges[node][j])));
      |                                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:62,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:214:44: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'std::__iterator_traits<__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >, void>::difference_type' {aka 'long int'})
  214 |                                 begin = max(begin, distance(nodes.begin(), find(nodes.begin(), nodes.end(), edges[node][j])));
      |                                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:11:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:214:44: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  214 |                                 begin = max(begin, distance(nodes.begin(), find(nodes.begin(), nodes.end(), edges[node][j])));
      |                                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~