#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g[n];
  vector<int> g2[n];
  for (int i = (0); i < (m); i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g2[b].push_back(a);
  }
  bool vis[n];
  memset(vis, false, sizeof(vis));
  stack<int> lis;
  for (int i = (0); i < (n); i++) {
    if (vis[i]) continue;
    stack<int> S;
    S.push(i);
    vis[i] = true;
    while (!S.empty()) {
      int c = S.top();
      for (auto u : g[c]) {
        if (!vis[u]) {
          S.push(u);
          vis[u] = true;
          break;
        }
      }
      if (c == S.top()) {
        lis.push(c);
        S.pop();
      }
    }
  }
  memset(vis, false, sizeof(vis));
  vector<vector<int> > scc;
  while (!lis.empty()) {
    int temp = lis.top();
    lis.pop();
    if (vis[temp]) continue;
    vis[temp] = true;
    stack<int> S;
    S.push(temp);
    vector<int> component;
    while (!S.empty()) {
      int c = S.top();
      S.pop();
      component.push_back(c);
      for (auto u : g2[c]) {
        if (!vis[u]) {
          vis[u] = true;
          S.push(u);
        }
      }
    }
    scc.push_back(component);
  }
  int id[n];
  for (int i = (0); i < (scc.size()); i++) {
    for (auto u : scc[i]) id[u] = i;
  }
  int k = scc.size();
  int ok[k];
  memset(ok, -1, sizeof(ok));
  int ans = 0;
  vector<int> cg[n];
  vector<int> rcg[n];
  for (int i = (0); i < (k); i++) {
    for (auto u : scc[i]) {
      for (auto e : g[u]) {
        if (id[u] == id[e]) continue;
        if (ok[id[e]] != i) {
          ok[id[e]] = i;
          cg[i].push_back(id[e]);
          cg[id[e]].push_back(i);
        }
      }
    }
  }
  memset(vis, false, sizeof(vis));
  for (int i = (0); i < (k); i++) {
    if (vis[i]) continue;
    int cnt = 0;
    queue<int> Q;
    Q.push(i);
    vis[i] = true;
    int VER = 0;
    bool cyc = false;
    while (!Q.empty()) {
      int c = Q.front();
      VER += scc[c].size();
      if (scc[c].size() > 1) cyc = true;
      Q.pop();
      cnt++;
      for (auto u : cg[c]) {
        if (!vis[u]) {
          vis[u] = true;
          Q.push(u);
        }
      }
    }
    if (cyc)
      ans += VER;
    else
      ans += VER - 1;
  }
  cout << ans << endl;
  return 0;
}
