#include <bits/stdc++.h>
using namespace std;
int n, m, cual;
vector<vector<int> > G, GT;
vector<int> scc;
vector<int> cuantosporssc;
vector<bool> ok;
stack<int> orden;
void dfs1(int curr) {
  if (not ok[curr]) {
    ok[curr] = true;
    for (int adj : G[curr]) dfs1(adj);
    orden.push(curr);
  }
}
void dfs2(int curr) {
  if (not ok[curr]) {
    ok[curr] = true;
    scc[curr] = cual;
    ++cuantosporssc[cual];
    for (int adj : GT[curr]) dfs2(adj);
  }
}
pair<int, bool> dfs3(int v) {
  if (not ok[v]) {
    pair<int, bool> ret = {1, cuantosporssc[scc[v]] > 1};
    ok[v] = true;
    for (int adj : G[v]) {
      auto rec = dfs3(adj);
      ret.first += rec.first;
      ret.second |= rec.second;
    }
    for (int adj : GT[v]) {
      auto rec = dfs3(adj);
      ret.first += rec.first;
      ret.second |= rec.second;
    }
    return ret;
  }
  return {0, false};
}
int main() {
  cin >> n >> m;
  G = vector<vector<int> >(n, vector<int>());
  GT = vector<vector<int> >(n, vector<int>());
  scc = vector<int>(n);
  cuantosporssc = vector<int>(n, 0);
  ok = vector<bool>(n, false);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    G[u].push_back(v);
    GT[v].push_back(u);
  }
  for (int i = 0; i < n; ++i)
    if (not ok[i]) dfs1(i);
  cual = 0;
  ok = vector<bool>(n, false);
  while (not orden.empty()) {
    int v = orden.top();
    orden.pop();
    if (not ok[v]) {
      dfs2(v);
      ++cual;
    }
  }
  ok = vector<bool>(n, false);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (not ok[i]) {
      pair<int, bool> cuantosyciclos = dfs3(i);
      if (cuantosyciclos.second)
        ans += cuantosyciclos.first;
      else
        ans += cuantosyciclos.first - 1;
    }
  }
  cout << ans << endl;
}
