#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct sx {
  int from, to, last;
};
struct sl {
  sx line[N], fl[2 * N];
  int last[N], color[N], sta[N], top, num, fnum, dfn[N], low[N], tot, colornum,
      fla[N], pointnum[N];
  bool c[N], cc[N];
  void add(int x, int y) {
    line[++num].to = y;
    line[num].from = x;
    line[num].last = last[x];
    last[x] = num;
  }
  void fadd(int x, int y) {
    fl[++fnum].to = y;
    fl[fnum].last = fla[x];
    fla[x] = fnum;
  }
  void dfs(int l) {
    c[l] = cc[l] = true;
    sta[++top] = l;
    dfn[l] = low[l] = ++tot;
    for (int j = last[l]; j > 0; j = line[j].last) {
      if (!c[line[j].to]) {
        dfs(line[j].to);
        low[l] = min(low[l], low[line[j].to]);
      } else if (cc[line[j].to])
        low[l] = min(low[l], dfn[line[j].to]);
    }
    if (low[l] == dfn[l]) {
      ++colornum;
      while (sta[top] != l) {
        color[sta[top]] = colornum;
        ++pointnum[colornum];
        cc[sta[top]] = false;
        --top;
      }
      color[l] = colornum;
      ++pointnum[colornum];
      cc[sta[top]] = false;
      --top;
    }
  }
  void tarjan(int n) {
    tot = 0;
    for (int i = 1; i <= n; ++i)
      if (!c[i]) dfs(i);
  }
  void addfl() {
    for (int i = 1; i <= num; ++i)
      if (color[line[i].from] != color[line[i].to]) {
        fadd(color[line[i].from], color[line[i].to]);
        fadd(color[line[i].to], color[line[i].from]);
      }
  }
} Tarjan;
int ans, n, m, z, x, y;
bool c[N];
int DFS(int l) {
  int ans = 0;
  c[l] = true;
  for (int j = Tarjan.fla[l]; j > 0; j = Tarjan.fl[j].last)
    if (!c[Tarjan.fl[j].to]) ans += DFS(Tarjan.fl[j].to);
  if (Tarjan.pointnum[l] > 1) ++ans;
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &x, &y);
    Tarjan.add(x, y);
  }
  Tarjan.tarjan(n);
  Tarjan.addfl();
  for (int i = 1; i <= Tarjan.colornum; ++i)
    if (Tarjan.pointnum[i] > 1) ans += Tarjan.pointnum[i];
  ans += Tarjan.colornum;
  for (int i = 1; i <= Tarjan.colornum; ++i)
    if (c[i] == false) {
      z = DFS(i);
      if (z > 0)
        ans -= z;
      else
        --ans;
    }
  printf("%d", ans);
}
