#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
int n;
int m;
int was[100000];
int was2[100000];
int use[100000];
vector<int> v[100000];
vector<int> rv[100000];
vector<int> u[100000];
vector<int> q, w;
int go(int x) {
  if (was[x]) return 0;
  was[x] = 1;
  for (int i = 0; i < ((int)(v[x]).size()); i++) go(v[x][i]);
  q.push_back(x);
  return 0;
}
int go2(int x) {
  if (use[x]) return 0;
  w.push_back(x);
  use[x] = 1;
  for (int i = 0; i < ((int)(u[x]).size()); i++) go2(u[x][i]);
  return 0;
}
int go3(int x) {
  if (was2[x]) return 0;
  int cur = 1;
  was2[x] = 1;
  for (int i = 0; i < ((int)(rv[x]).size()); i++) cur += go3(rv[x][i]);
  return cur;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    v[a].push_back(b);
    rv[b].push_back(a);
    u[a].push_back(b);
    u[b].push_back(a);
  }
  int ans = n;
  for (int i = 0; i < n; i++)
    if (!use[i]) {
      w.clear();
      go2(i);
      q.clear();
      for (int j = 0; j < ((int)(w).size()); j++)
        if (!was[w[j]]) go(w[j]);
      reverse((q).begin(), (q).end());
      int ok = 1;
      for (int j = 0; j < ((int)(q).size()); j++)
        if (go3(q[j]) > 1) {
          ok = 0;
          break;
        }
      ans -= ok;
    }
  printf("%d\n", ans);
  return 0;
}
