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
#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
#define VAR(x) #x ": " << x << " "
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define gcd __gcd
#define x first
#define y second
#define st first
#define nd second
#define pb push_back

using namespace std;

template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; }
template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; }

typedef long long int64;
typedef pair<int, int> PII;
typedef pair<int64, int64> PLL;
typedef long double K;
typedef vector<int> VI;

const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};

const int INF = 1000 * 1000 * 1000;

const int T = 18;

int n, m, s;
int ord[500010];
vector<int> in[500010];
vector<int> out[500010];
int color[500010];
int times[500010];

bool cmpT(int a, int b){
	if(times[a] == times[b]) return ord[a] < ord[b];
	return times[a] < times[b];
}

vector<int> *graph;

int dfs(int u){
	color[u] = 1;
	for(int v : graph[u]){
		if(color[v] == 1){
			s = v;
			++times[u];
			return 1;
		}
		if(color[v] == 0){
			int state = dfs(v);
			if(state == 1){
				++times[u];
				return s == u ? 2 : 1;
			}
			if(state == 2)
				return 2;
		}
	}
	color[u] = 2;
	return 0;
}

int main(){
	scanf("%d %d", &n, &m);
	FWD(i,0,m){
		int a, b;
		scanf("%d %d", &a, &b);
		out[a].push_back(b);
		in[b].push_back(a);
	}

	graph = out;

	int t = 0;
	FWD(i,1,n+1){
		ord[i-1] = i;
		if(color[i] == 0 && dfs(i) == 2)
			++t;
	}
	if(t == 0){
		printf("NIE\n");
		return 0;
	}
	if(t == 2){
		printf("0\n\n");
		return 0;
	}

	FWD(r,1,T){
		FWD(i,1,n+1){
			color[i] = 0;
			random_shuffle(graph[i].begin(), graph[i].end());
		}
		random_shuffle(ord, ord+n);
		FWD(i,0,n){
			if(color[ord[i]] == 0 && dfs(ord[i]) == 2)
				break;
		}
	}

	FWD(r,0,T){
		FWD(i,1,n+1){
			color[i] = 0;
			sort(graph[i].begin(), graph[i].end(), cmpT);
		}
		random_shuffle(ord, ord+n);
		FWD(i,0,n){
			if(color[ord[i]] == 0 && dfs(ord[i]) == 2)
				break;
		}
	}

	graph = in;

	FWD(r,0,T){
		FWD(i,1,n+1){
			color[i] = 0;
			random_shuffle(graph[i].begin(), graph[i].end());
		}
		random_shuffle(ord, ord+n);
		FWD(i,0,n){
			if(color[ord[i]] == 0 && dfs(ord[i]) == 2)
				break;
		}
	}

	FWD(r,0,T){
		FWD(i,1,n+1){
			color[i] = 0;
			sort(graph[i].begin(), graph[i].end(), cmpT);
		}
		random_shuffle(ord, ord+n);
		FWD(i,0,n){
			if(color[ord[i]] == 0 && dfs(ord[i]) == 2)
				break;
		}
	}

	vector<int> V;
	FWD(i,1,n+1) if(times[i] == 4*T) V.push_back(i);
	printf("%d\n", SIZE(V));
	for(int u : V) printf("%d ", u);
	printf("\n");
	return 0;
}