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
#include<cstdio>
#include<queue>
#include<vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

#define MAX_V 500000

struct cmp {
  bool operator()(vector<int> * const& a, vector<int> * const& b) {
    if (a->size() == b->size()) {
      return *a < *b;
    }
    return a->size() < b->size();
  }
};

vector<int> graph[MAX_V];
vector<char> color;
vector<int> back_passing, back_entering;
int back;

void dfs(int v) {
  color[v] = 1;
  for (int neighbour : graph[v]) {
    if (!color[neighbour]) {
      dfs(neighbour);
      back_passing[v] += back_passing[neighbour] - back_entering[neighbour];
    } else if (color[neighbour] == 1) {
      ++back;
      back_passing[v]++;
      back_entering[neighbour]++;
    }
  }
  color[v] = 2;
}

bool dag_dfs(int v) {
  color[v] = 1;
  for (int neighbour : graph[v]) {
    if (!color[neighbour]) {
      if (!dag_dfs(neighbour)) {
        return false;
      }
    } else if (color[neighbour] == 1) {
      return false;
    }
  }
  color[v] = 2;
  return true;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  REP(i, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    graph[a].push_back(b);
  }
  color = vector<char>(n, 0);
  back_entering = back_passing = vector<int>(n, 0);
  back = 0;
  REP(i, n) {
    if (!color[i]) {
      dfs(i);
    }
  }
  vector<char> result(n, 0);
  REP(i, n) {
    if (back_passing[i] < back || back == 0) {
      result[i] = 1;
    }
  }
  REP(i, n) {
    if (result[i] == 0) {
      color = vector<char>(n, 0);
      color[i] = 2;
      bool is_dag = true;
      REP(j, n) {
        if (!color[j]) {
          is_dag &= dag_dfs(j);
          if (!is_dag) {
            break;
          }
        }
      }
      result[i] = (is_dag ? 2 : 1);
      if (graph[i].size() == 2) {
        queue<int> Q;
        Q.push(i);
        while (Q.empty()) {
          int q = Q.front();
          Q.pop();
          for ( int v : graph[q]) {
            if ( graph[v].size() == 2) {
              result[v] = result[q];
              Q.push(v);
            }
          }
        }
      }
    }
  }
  int kontra_cnt = 0;
  REP(i, n) {
    if (result[i] == 2) {
      kontra_cnt++;
    }
  }
  if ( kontra_cnt == 0 ) {
    printf("NIE\n");
  } else {
    printf("%d\n", kontra_cnt);
    REP(i, n) {
      if (result[i] == 2) {
        printf("%d ", i + 1);
      }
    }
    printf("\n");
  }
  return 0;
}