#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n, m, ans, l, r;
int line[MAXN];
int du[MAXN];
int nxt[MAXN];
bool boo[MAXN];
bool bbb[MAXN];
vector<int> v[MAXN];
int Find(int k) {
  if (nxt[k] != k) nxt[k] = Find(nxt[k]);
  return nxt[k];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) nxt[i] = i;
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    int xx = Find(x), yy = Find(y);
    if (xx != yy) nxt[xx] = yy;
    ++du[y];
  }
  ans = n;
  for (int i = 1; i <= n; ++i) {
    int tmp = Find(i);
    if (!boo[tmp]) {
      boo[tmp] = true;
      --ans;
    }
  }
  memset(boo, 0, sizeof(boo));
  for (int i = 1; i <= n; ++i)
    if (!du[i]) line[++r] = i;
  for (l = 1; l <= r; ++l) {
    bbb[line[l]] = true;
    for (__typeof(v[line[l]].begin()) p = v[line[l]].begin();
         p != v[line[l]].end(); ++p) {
      --du[*p];
      if (!du[*p]) line[++r] = *p;
    }
  }
  for (int i = 1; i <= n; ++i)
    if (!bbb[i]) {
      int tmp = Find(i);
      if (!boo[tmp]) {
        boo[tmp] = true;
        ++ans;
      }
    }
  printf("%d\n", ans);
  return 0;
}
