#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 14;
vector<int> uadj[MAXN], adj[MAXN], vcomp;
bool cycle;
int visited[MAXN], vvisited[MAXN];
void uDfs(int v) {
  visited[v] = 1;
  vcomp.push_back(v);
  for (auto u : uadj[v]) {
    if (visited[u] == 0) uDfs(u);
  }
}
void dfs(int v) {
  vvisited[v] = 1;
  for (auto u : adj[v]) {
    if (vvisited[u] == 0)
      dfs(u);
    else if (vvisited[u] == 1)
      cycle = true;
  }
  vvisited[v] = 2;
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    uadj[a].push_back(b);
    uadj[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      uDfs(i);
      for (auto u : vcomp) {
        if (vvisited[u] == 0) dfs(u);
      }
      ans += vcomp.size() - (cycle ^ 1);
      cycle = false;
      vcomp.clear();
    }
  }
  cout << ans << '\n';
  return 0;
}
