#include <bits/stdc++.h>
using namespace std;
const int mxn = 100 * 1000 + 5;
int n, m, ans;
vector<int> g[mxn], rg[mxn], topol;
bool been[mxn], cycle[mxn];
void topol_sort(int u) {
  been[u] = true;
  for (int v : g[u])
    if (!been[v]) topol_sort(v);
  topol.push_back(u);
}
void scc(int u, bool q) {
  been[u] = true;
  cycle[u] = q;
  for (int v : rg[u])
    if (!been[v]) cycle[u] = true, scc(v, true);
}
bool wcc(int u) {
  been[u] = true;
  bool res = cycle[u];
  for (int v : g[u])
    if (!been[v]) res |= wcc(v);
  for (int v : rg[u])
    if (!been[v]) res |= wcc(v);
  return res;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    rg[b].push_back(a);
  }
  for (int i = 1; i <= n; i++)
    if (!been[i]) topol_sort(i);
  reverse(topol.begin(), topol.end());
  fill(been, been + n + 3, false);
  for (int v : topol)
    if (!been[v]) scc(v, false);
  fill(been, been + n + 3, false);
  int DAGCOMP = 0, CYCLE = 0;
  for (int v : topol)
    if (!been[v]) {
      if (wcc(v)) CYCLE++;
      DAGCOMP++;
    }
  ans += n - DAGCOMP;
  ans += CYCLE;
  cout << ans;
  return 0;
}
