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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#ifdef LOCAL
#define debug(x) {cerr <<#x<<" = " <<x<<"\n"; }
#define debug2(x, y) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, t) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#t <<" = " <<t<<"\n";}
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x,y,z,t)
#define debugv(x)
#define cerr if(0)cout
#endif
#define make(type, x) type x; cin>>x;
#define make2(type, x, y) type x, y; cin>>x>>y;
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z;
#define make4(type, x, y, z, t) type x, y, z, t; cin>>x>>y>>z>>t;
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
using namespace std;
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }

const int N = 5e5 + 5;
int inv[N];
int cnt_bottle[N];

// Przeklejam jakis stary kod tutaj, widac ze nie za piekny :P
// Byc moze tez przekleje kod Grzesia stad: http://codeforces.com/gym/100298/submission/5287802
struct Graph {
  VVI slo;
  VVI trans;
  VI vis, ktop, ss;
  int d, licz, n, duze_cnt, ost_duze;
  VVI v_in_ss;
  VI Q;
  Graph(int n_) {
    n = n_;
    slo.resize(n + 5);
    trans.resize(n + 5);
    vis.resize(n + 5);
    ktop.resize(n + 5);
    ss.resize(n + 5);
    v_in_ss.resize(n + 5);
    V.resize(n + 5);
    I.resize(n + 5);
    Q.resize(n + 5);
    d = 0;
    duze_cnt = 0;
  }
  void AddEdge(int a, int b) {
    slo[a].PB(b);
    trans[b].PB(a);
  }
  
  void dfs1(int w) // liczy postorder^{-1}, tzn jak post[v]=d, to ktop[d]=w
  {
    vis[w]=1;
    for(int g=0; g<slo[w].size(); g++)
    {
      if(vis[slo[w][g]]==0)
      {
        dfs1(slo[w][g]);
      }
    }
    d++;
    ktop[d]=w;
  }
  void dfs2(int v) // lazi po jednej silnie spojnej
  {
    ss[v]=licz;
    v_in_ss[licz].PB(v);
    for(int i=0; i<trans[v].size(); i++)
    {
      if(ss[trans[v][i]]==0)
      {
        dfs2(trans[v][i]);
      }
    }
  }
  string podziel(Graph& G, int fir) { // przypisuje wierzcholkom numer ich silnie spojnej i zwraca silnie spojna o ile istnieje dokladnie 1 wieksza niz 1
    for(int i=1; i<=n; i++)
    {
      if(vis[i]==0)
      {
        dfs1(i);
      }
    }
    licz=0;
    for(int i=n; i>0; i--) // idzie od najwiekszych numerow postorder
    {
      if(ss[ktop[i]]==0) // jezeli ktop[i] nie jest przydzielony do zadnej, to pomaluj jego silnie spojna
      {
        licz++;
        dfs2(ktop[i]);
        if (SZ(v_in_ss[licz]) > 1) {
          duze_cnt++;
          ost_duze = licz;
        }
      }
    }
    if (duze_cnt > 1) {
      return "DUZO";
    }
    if (duze_cnt == 0) {
      return "DAG";
    }
    G = Graph(SZ(v_in_ss[ost_duze]));
    VI new_nr(n + 5);
    for (int i = 0; i < SZ(v_in_ss[ost_duze]); i++) {
      new_nr[v_in_ss[ost_duze][i]] = i + 1;
      if (fir) {
        inv[i + 1] = v_in_ss[ost_duze][i];
      }
    }
    for (auto v : v_in_ss[ost_duze]) {
      for (auto nei : slo[v]) {
        if (new_nr[nei]) {
          G.AddEdge(new_nr[v], new_nr[nei]);
        }
      }
    }
    return "OK";
  }
  
  std::vector<bool> V;
  std::vector<int> I;
  VI P;
  bool find(int v)
  {
    if(V[v]) return 0; V[v] = 1;
    if(v==n){ I[v] = 1; P.push_back(v); return 1; }
    REP(i,slo[v].size()) if(find(slo[v][i]))
    {
      std::swap(slo[v][i],slo[v].back());
      slo[v].pop_back();
      I[v] = P.size()+1;
      P.push_back(v);
      return 1;
    }
    return 0;
  }
  
  
  void Bottlenecks(bool transs) {
    if (transs) {
      swap(slo, trans);
    }
    n++;
    RE (v, n) {
      for (int i = 0; i < SZ(slo[v]); i++) {
        if (slo[v][i] == 1) {
          slo[v][i] = n;
        }
      }
    }
    assert(find(1));
    V.clear();
    V.resize(n + 5);
    //debugv(P);
    int qf=0,ql=0;
    VI R;
    int faza = 1;
    VVI fazy(n + 5);
    while(P.size()) {
      int b = P.size();
      while(qf<ql) {
        int v = Q[qf++];
        if(I[v]) {
          if(I[v]<b) {
            b = I[v];
          } 
        } else {
          for(int u : slo[v]) {
            if(!V[u]) {
              V[u] = faza;
              fazy[faza].PB(u);
              Q[ql++] = u;
            }
          }
        }
      }
      qf = ql = 0;
      if(b==P.size())
      {
        R.push_back(P.back());
        //debug(P.back());
        b--;
        if (V[P.back()] == 0) {
          V[P.back()] = faza;
          fazy[faza].PB(P.back());
        }
        //cerr<<"fazy["<<faza<<"] = ";
        for (auto v : fazy[faza]) {
          if (v == 1) { continue; }
          for (auto prev : trans[v]) {
            if (V[prev] == 0) {
              //cerr<<"(Emptuj bo "<<prev<<" z "<<v<<") ";
              R = VI{R.back()};
            }
          }
          //cerr<<v<<" ";
        }
        //cerr<<endl;
        faza++;
      }
      while(P.size()>b)
      {
        I[P.back()] = 0;
        if (V[P.back()] == 0) {
          V[P.back()] = faza;
          fazy[faza].PB(P.back());
        }
        Q[ql++] = P.back();
        P.pop_back();
      }
    }
    assert(R.back() == n);
    R.pop_back();
    //debugv(R);
    for (auto x : R) {
      cnt_bottle[x]++;
    }
  }
  void print() {
    //cerr<<"G: "<<endl;
//     RE (i, n) {
//       for (auto nei : slo[i]) {
//         cerr<<i<<" "<<nei<<endl;
//       }
//     }
//     cerr<<"juz"<<endl;
  }
  bool Acyc(int forb) {
    VI indeg(n + 5);
    RE (i, n) {
      indeg[i] = 0;
    }
    RE (i, n) {
      for (auto nei : slo[i]) {
        if (nei == forb || i == forb) { continue; }
        indeg[nei]++;
      }
    }
    VI que;
    RE (i, n) {
      if (i != forb && indeg[i] == 0) {
        que.PB(i);
      }
    }
    //debug(forb);
  // debugv(que);
    for (int ii = 0; ii < SZ(que); ii++) {
      int v = que[ii];
      for (auto nei : slo[v]) {
        if (nei == forb) { continue; }
        indeg[nei]--;
        if (indeg[nei] == 0) {
          que.PB(nei);
        }
      }
    }
    return SZ(que) == n - !!forb;
  }
};

void Bottlenecks(Graph G, bool trans) {
  G.Bottlenecks(trans);
}
VI slo[N];
int vis[N];
int indeg[N];
bool Acyc(int forb, int n) {
  RE (i, n) {
    vis[i] = 0;
    indeg[i] = 0;
  }
  RE (i, n) {
    for (auto nei : slo[i]) {
      if (nei == forb || i == forb) { continue; }
      indeg[nei]++;
    }
  }
  VI que;
  RE (i, n) {
    if (i != forb && indeg[i] == 0) {
      que.PB(i);
    }
  }
  //debug(forb);
 // debugv(que);
  for (int ii = 0; ii < SZ(que); ii++) {
    int v = que[ii];
    for (auto nei : slo[v]) {
      if (nei == forb) { continue; }
      indeg[nei]--;
      if (indeg[nei] == 0) {
        que.PB(nei);
      }
    }
  }
  return SZ(que) == n - !!forb;
}
#undef int
int main() {
#define int long long

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  
  //brutoheura
  
  make2(int, n, m);
  if (n + m <= 2000) {
    RE (i, m) {
      make2(int, a, b);
      slo[a].PB(b);
    }
    if (Acyc(0, n)) {
      cout<<"NIE"<<endl;
      return 0;
    }
    VI res;
    RE (i, n) {
      if (Acyc(i, n)) {
        res.PB(i);
      }
    }
    cout<<SZ(res)<<endl;
    for (auto v : res) {
      cout<<v<<" ";
    }
    cout<<endl;
  } else {
  
  
  
  Graph G1(n);
  RE (i, m) {
    make2(int, a, b);
    G1.AddEdge(a, b);
  }
  Graph G2(0);
  string str = G1.podziel(G2, 1);
  if (str == "DAG") {
    cout<<"NIE\n";
    return 0;
  }
  if (str == "DUZO") {
    cout<<"0\n\n";
    return 0;
  }
  G2.print();
  Bottlenecks(G2, 0);
  Bottlenecks(G2, 1);
  VI res;
  int fir = -1;
  RE (i, n) {
    if (cnt_bottle[i] == 2) {
      fir = i;
      res.PB(inv[i]);
    }
  }
  if (!res.empty() && !G2.Acyc(fir)) {
    cerr<<"Ostatnia chwila"<<endl;
    cout<<"0\n\n";
    return 0;
  }
  sort(ALL(res));
  cout<<SZ(res)<<endl;
  for (auto v : res) {
    cout<<v<<" ";
  }
  cout<<endl;
  
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  return 0;
}