#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> g[1000000];
vector<int> gdir[1000000];
int aridadin[1000000];
int visto[1000000];
void genera(int u, int &numnod, queue<int> &q) {
  if (visto[u]) return;
  visto[u] = 1;
  numnod++;
  vector<int> &ar = g[u];
  if (aridadin[u] == 0) q.push(u);
  for (int i = 0; i < int(ar.size()); i++) {
    int v = ar[i];
    genera(v, numnod, q);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
    gdir[u].push_back(v);
    aridadin[v]++;
  }
  int sol = 0;
  for (int i = 1; i <= n; i++) {
    int u = i;
    if (not visto[u]) {
      int numnod = 0;
      queue<int> q;
      genera(u, numnod, q);
      int amount = 0;
      while (not q.empty()) {
        u = q.front();
        q.pop();
        amount++;
        vector<int> &ar = gdir[u];
        for (int i = 0; i < int(ar.size()); i++) {
          int v = ar[i];
          aridadin[v]--;
          if (aridadin[v] == 0) q.push(v);
        }
      }
      if (amount == numnod)
        sol += numnod - 1;
      else
        sol += numnod;
    }
  }
  cout << sol << endl;
}
