1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include<cstdio>
#include<algorithm>
#include<utility>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>

#define FORE(c, a, b) for(int c=a; c<= int(b); ++c)
#define FORD(c, a, b) for(int c=a; c>= int(b); --c)
#define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ALL(a) a.begin(), a.end() 
#define PR(n) printf("%d ", (int) (n)) 
#define PRL(n) printf("%lld ", (ll) (n)) 
#define SC(n) scanf("%d ", &n) 

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define itr iterator

#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;

typedef vector<int> vi;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<pi> vii;
typedef vector<pii> vpii;
typedef set<int> si;
typedef set<pi> spi;

const int NMAX = 600 * 1000 + 9;

int n, m, x, y, k;
int isCycle, vis[NMAX], cyclePos[NMAX], minPos[NMAX], f[NMAX];
vi edges[NMAX], cycle, ans;

int isShifted(vi & a, vi & b) { // taki sam size
	if(a.empty()) return 1;
	int i = 0, aa = a.size();
	while(i < aa && a[i] != b[0]) i++;
	if(i == aa) return 0;
	
	REP(j, aa) {
		int jj = j + i;
		jj = jj >= aa ? jj - aa : jj;
		if(a[jj] != b[j]) return 0;
	}
	return 1;
}

int myDist(int a, int b) {
	a = a + k - b;
	return a >= k ? a - k : a;
}

int myRet(int v, int w) {
	prd("add %d\n", v);
	cycle.pb(v);
	vis[v] = 2;
	return w;
}

int dfs_cycle(int v) {
// -1: finish, NMAX: no cycle, w: unfolding started in w
	prd("dfs_cycle %d\n", v);
	vis[v] = 1;
	FORIT(it, edges[v], vi) {
		int w = *it;
		if(vis[w] == 1) {
			return myRet(v, w);
		} else if(vis[w] == 2)
			continue;
			
		int ret = dfs_cycle(w);
		if(ret == -1) {
			vis[v] = 2;
			return -1;
		} else if(ret == v) {
			return myRet(v, -1);
		} else if(ret < NMAX) {
			return myRet(v, ret);
		} // if NMAX go on
	}
	vis[v] = 2;
	return NMAX;
}
		
void update(int & xv, int xw, int start) {
	prd("update %d %d %d = ", xv, xw, start);
	if(xv == -1 || xw == NMAX)
		return;
	if(xw == -1 || xv == NMAX)
		xv = xw;
	else
		xv = myDist(xw, start) < myDist(xv, start) ? xw : xv;
}		
		
void dfs_afap(int v, int start) {
	prd("dfs_afap %d (%d) \n", v, start);
	if(cyclePos[v] >= 0 && cyclePos[v] != start)
		return;
	
	vis[v] = 1;	
	minPos[v] = NMAX;
	FORIT(it, edges[v], vi) {
		int w = *it;
		if(vis[w] == 1) {
			if(cyclePos[w] >= 0) { // doszedlem so siebie
				if(cyclePos[w] != start)
					prd("ALERT SHIT\n");				
			} else { // znalazlem niezalezny cykl
				prd("found cycle\n");
				minPos[v] = -1;
			}
		}
		if(vis[w] == 0)
			dfs_afap(w, start);
		update(minPos[v], cyclePos[w] >= 0 ? cyclePos[w] : minPos[w], start);
		prd("%d\n", minPos[v]);
	}
	prd("minPos[%d] = %d\n", v, minPos[v]);
	vis[v] = 2;
}

int dfs_tree(int v) {
	prd("dfs_tree %d\n", v);
	// -1: finish, NMAX: no cycle, w: unfolding started in w
	vis[v] = 1;
	if(vis[f[v]] == 1) {
		prd("ans: %d\n", cycle[v]);
		ans.clear(); // olewamy poprzedni i tak wyjdzie zaraz ze sa 2
		ans.pb(cycle[v]);
		vis[v] = 2;
		return v == f[v] ? -1 : f[v];
	}
	if(vis[f[v]] == 2) {
		vis[v] = 2;
		return NMAX;
	}
	int ret = dfs_tree(f[v]);
	vis[v] = 2;
	
	if(ret == -1 || ret == NMAX)
		return ret;
	if(ret == v)
		ret = -1;
	prd("ans: %d\n", cycle[v]);
	ans.pb(cycle[v]);
	return ret;
}

int cycleToAns() {
// wystarcza wierzcholki od konca na vi: cycle
	FORE(i, 1, n) {
		vis[i] = 0;
		cyclePos[i] = -1;
	}
	REP(ci, cycle.size())
		cyclePos[cycle[ci]] = ci;
	k = cycle.size();
	
// oblicz f dla [0, k)
	REP(ci, cycle.size()) { // cykl jest od konca
		int cand = cycle[ci];
		prd("cand %d\n", cand);
		
		dfs_afap(cand, ci);
		if(minPos[cand] == -1)
			return 1;
		f[ci] = minPos[cand];
		prd("f[%d] = %d\n", ci, f[ci]);
	}
	
	// oblicz cykl dla f
	ans.clear();
	REP(i, k) {
		prd("f[%d] = %d     g[%d] = %d\n", i, f[i], cycle[i], cycle[f[i]]);
		vis[i] = 0;
	}
	REP(i, k)
		if(!vis[i])
			dfs_tree(i);
	return 0;
}

int main() {
	scanf("%d%d", &n, &m);
	REP(i, m) {
		scanf("%d%d", &x, &y);
		edges[x].pb(y);
	}
	
	// znajdz initial cycle
	FORE(i, 1, n)
		vis[i] = 0;
	FORE(i, 1, n)
		if(cycle.empty() && !vis[i])
			dfs_cycle(i);
	if(cycle.empty()) {
		printf("NIE");
		return 0;
	}
	
	while(1) {
		if(cycleToAns()) {
			printf("0\n\n");
			return 0;
		}
		/*FORIT(it, ans, vi) printf("%d ", *it);
		printf("(%d %d %d)\n", (int) ans.size(), (int) cycle.size(), isShifted(ans, cycle));*/
		if(ans.size() == cycle.size()) {
			if(isShifted(ans, cycle))
				break;
			else {
				printf("0\n\n");
				return 0;
			}
		}
		cycle = ans;
	}
	
	// sprawdz czy nie ma jakiegos drugiego cyklu
	FORE(i, 1, n)
		vis[i] = 0;
	FORIT(it, ans, vi)
		vis[*it] = 2;
	cycle.clear();
	FORE(i, 1, n)
		if(!vis[i])
			dfs_cycle(i);
	if(!cycle.empty()) {
		printf("0\n\n");
		return 0;
	}
	
	sort(ALL(ans));
	printf("%d\n", (int) ans.size());
	FORIT(it, ans, vi)
		printf("%d ", *it);
	return 0;
}