#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200002;
vector<int> e[MAXN];
vector<int> eo[MAXN];
bool w[MAXN];
int cnt = 0;
vector<int> tout;
int g[MAXN], gc;
void dfs(int v) {
  if (w[v]) return;
  w[v] = true;
  for (int i = 0; i < e[v].size(); i++) {
    dfs(e[v][i]);
  }
  tout.push_back(v);
}
void dfs2(int v) {
  if (w[v]) return;
  w[v] = true;
  cnt++;
  g[v] = gc;
  for (int i = 0; i < eo[v].size(); i++) {
    dfs2(eo[v][i]);
  }
}
vector<int> c3;
void dfs3(int v) {
  if (w[v]) return;
  w[v] = true;
  c3.push_back(v);
  for (int i = 0; i < e[v].size(); i++) {
    dfs3(e[v][i]);
  }
}
bool cmp(int a, int b) { return (g[a] < g[b]); }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    eo[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) w[i] = false;
  for (int i = 1; i <= n; i++) {
    dfs(i);
  }
  for (int i = 1; i <= n; i++) w[i] = false;
  for (int i = n - 1; i >= 0; i--) {
    cnt = 0;
    if (!w[tout[i]]) {
      gc = i;
      dfs2(tout[i]);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < eo[i].size(); j++) {
      e[i].push_back(eo[i][j]);
    }
  }
  int ans = n;
  for (int i = 1; i <= n; i++) w[i] = false;
  for (int i = 1; i <= n; i++) {
    if (!w[i]) {
      ans--;
      c3.clear();
      dfs3(i);
      sort(c3.begin(), c3.end(), cmp);
      for (int j = 0; j + 1 < c3.size(); j++) {
        if (g[c3[j]] == g[c3[j + 1]]) {
          ans++;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
