#include <bits/stdc++.h>
using namespace std;
vector<int> mg[100005], um[100005];
int marka[100005], pos[100005], uk, niz[100005], f;
void dfs(int cvor) {
  int i, x;
  pos[cvor] = 1;
  uk++;
  niz[uk] = cvor;
  for (i = 0; i < mg[cvor].size(); i++) {
    x = mg[cvor][i];
    if (pos[x] == 0) dfs(x);
  }
}
void nciklus(int cvor) {
  int i, x;
  marka[cvor] = 1;
  for (i = 0; i < um[cvor].size(); i++) {
    x = um[cvor][i];
    if (marka[x] == 1) {
      f = 1;
    } else if (marka[x] == 0)
      nciklus(x);
  }
  marka[cvor] = 2;
}
int main() {
  int n, m, i, j, k, a, b, t, res = 0, x;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> a >> b;
    mg[a].push_back(b);
    mg[b].push_back(a);
    um[a].push_back(b);
  }
  for (i = 1; i <= n; i++) {
    if (pos[i] == 0) {
      uk = 0;
      dfs(i);
      f = 0;
      for (j = 1; j <= uk; j++) {
        x = niz[j];
        if (marka[x] == 0) nciklus(x);
      }
      if (f)
        res = res + uk;
      else
        res = res + uk - 1;
    }
  }
  cout << res << endl;
  return 0;
}
