#include <bits/stdc++.h>
using namespace std;
int n, m, dad[100010], x, y, gs[100010], head[200010], ne[1000010], v[1000010],
    vis[200010], l, use[200010], i, ans, tot;
void add(int x, int y) {
  v[++l] = y;
  ne[l] = head[x];
  head[x] = l;
}
int find(int x) { return dad[x] == x ? x : dad[x] = find(dad[x]); }
int dfs(int x) {
  vis[x] = 1;
  tot++;
  for (int i = head[x]; i; i = ne[i]) {
    if (!vis[v[i]])
      if (!dfs(v[i])) return 0;
    if (vis[v[i]] == 1) return 0;
  }
  vis[x] = -1;
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) dad[i] = i;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    gs[y]++;
    if (find(x) != find(y)) dad[dad[x]] = dad[y];
  }
  for (i = 1; i <= n; i++) {
    use[find(i)]++;
    if (!gs[i]) add(dad[i] + n, i);
  }
  for (i = 1; i <= n; i++)
    if (use[i]) {
      ans += use[i];
      tot = 0;
      if (dfs(i + n) && tot > use[i] && head[i + n]) ans--;
    }
  printf("%d\n", ans);
}
