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
#include<bits/stdc++.h>
#define FOR(i,s,e) for(int i=(s);i<=(e);i++)
#define FORD(i,s,e) for(int i=(s);i>=(e);i--)
#define ALL(k) (k).begin(),(k).end()
#define e1 first 
#define e2 second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<int,PII> PIP;
typedef pair<PLL,PLL> PPP;
const LL TYS=1000;
const LL MLD=TYS*TYS*TYS;
const int MAXN=500111;

vector<int> wyj[MAXN],wej[MAXN];
int deg[MAXN];
int usu[MAXN];
int deg2[MAXN];
void czysc(int n){
	queue<int> Q;
	FOR(v,1,n){
		deg[v]=deg2[v]=0;
		if(usu[v]) continue;
		for(auto b:wej[v]) if(usu[b]==0) deg[v]++;
		for(auto b:wyj[v]) if(usu[b]==0) deg2[v]++;
		if(deg[v]==0||deg2[v]==0) Q.push(v);
	}
	while(!Q.empty()){
		int a=Q.front();Q.pop();
		if(usu[a]) continue;
		usu[a]=1;
		deg[a]=deg2[a]=0;
		for(auto b:wej[a]){
			if(usu[b]) continue;
			deg2[b]--;
			if(deg2[b]==0) Q.push(b);
		}
		for(auto b:wyj[a]){
			if(usu[b]) continue;
			deg[b]--;
			if(deg[b]==0) Q.push(b);
		}
	}
}
		

int dagquery(int n){
	queue<int> Q;
	FOR(v,1,n){
		deg[v]=0;
		if(usu[v]) continue;
		for(auto b:wej[v]) if(usu[b]==0) deg[v]++;
		if(deg[v]==0) Q.push(v);
	}
	while(!Q.empty()){
		int a=Q.front();Q.pop();
		for(auto b:wyj[a]){
			if(usu[b]) continue;
			deg[b]--;
			if(deg[b]==0) Q.push(b);
		}
	}
	FOR(v,1,n) if(deg[v]>0) return 0;
	return 1;
}
int ojc[MAXN];
int odw[MAXN];
int dfs(int v,int st){
	for(auto b:wyj[v]){
		if(usu[b]) continue;
		if(b==st){
			ojc[st]=v;
			return 1;
		}
		if(odw[b]) continue;
		odw[b]=1;
		ojc[b]=v;
		int k=dfs(b,st);
		if(k) return 1;
	}
	return 0;
}
		
	
vector<int> cykl;

vector<int> ans;
main(){
	srand(696969);
	int n,m;scanf("%d%d",&n,&m);
	FOR(i,1,m){
		int a,b;scanf("%d%d",&a,&b);
		wyj[a].PB(b);
		wej[b].PB(a);
	}
	FOR(i,1,n){
		random_shuffle(ALL(wyj[i]));
		random_shuffle(ALL(wej[i]));
	}
	czysc(n);
	int k=dagquery(n);
	if(k) {puts("NIE");return 0;}
	FOR(i,1,n){
		if(deg[i]>0){
			dfs(i,i);
			int ob=i;
			cykl.PB(ob);
			ob=ojc[ob];
			while(ob!=i){
				cykl.PB(ob);
				ob=ojc[ob];
			}
			break;
		}
	}
	for(auto it:cykl){
		usu[it]=1;
		if(dagquery(n)) ans.PB(it);
		usu[it]=0;
	}
	sort(ALL(ans));
	printf("%d\n",(int)ans.size());
	for(auto it:ans) printf("%d ",it);
	puts("");
}