#include <bits/stdc++.h>
using namespace std;
vector<int> v[100010];
int p[100010], mark[100010], in[100010];
queue<int> que;
int Find(int x) { return p[x] == x ? x : (p[x] = Find(p[x])); }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) p[i] = i;
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    in[y]++;
    p[Find(x)] = Find(y);
  }
  for (int i = 1; i <= n; i++)
    if (in[i] == 0) que.push(i);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    for (int k = 0; k < (v[i].size()); k++) {
      int to = v[i][k];
      if (--in[to] == 0) que.push(to);
    }
  }
  for (int i = 1; i <= n; i++)
    if (in[i]) mark[Find(i)] = 1;
  int ans = n;
  for (int i = 1; i <= n; i++)
    if (Find(i) == i && mark[Find(i)] == 0) ans--;
  printf("%d\n", ans);
  return 0;
}
