#include <bits/stdc++.h>
using namespace std;
const int MN = 1e5 + 100;
vector<int> v1[MN];
vector<int> v2[MN];
vector<int> group;
bool used1[MN];
bool used2[MN];
bool used3[MN];
void findGroup(int x) {
  used1[x] = 1;
  group.push_back(x);
  for (auto y : v1[x]) {
    if (!used1[y]) {
      findGroup(y);
    }
  }
}
int findCycle(int x) {
  used2[x] = 1;
  used3[x] = 1;
  for (auto y : v2[x]) {
    if (used3[y]) return 1;
    if (!used2[y]) {
      if (findCycle(y)) return 1;
    }
  }
  used3[x] = 0;
  return 0;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int q, w;
    cin >> q >> w;
    --q, --w;
    v1[q].push_back(w);
    v1[w].push_back(q);
    v2[q].push_back(w);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (!used1[i]) {
      group.clear();
      findGroup(i);
      if (group.size() == 1) continue;
      for (int j = 0; j < group.size(); ++j) {
        if (!used2[group[j]]) {
          if (findCycle(group[j])) goto lol;
        }
      }
      ans += group.size() - 1;
      continue;
    lol:;
      ans += group.size();
    }
  }
  cout << ans << '\n';
}
