#include <bits/stdc++.h>
using namespace std;
int n, m, ok, k, v[100005], x, y, ans;
vector<int> a[100005], b[100005], c;
bitset<100005> w;
void udfs(int x) {
  w[x] = 1;
  vector<int>::iterator it;
  c.push_back(x);
  for (it = b[x].begin(); it != b[x].end(); ++it)
    if (!w[*it]) udfs(*it);
}
void dfs(int x) {
  v[x] = 2;
  vector<int>::iterator it;
  for (it = a[x].begin(); it != a[x].end() && !ok; ++it) {
    if (v[*it] == 2)
      ok = 1;
    else if (!v[*it])
      dfs(*it);
  }
  k++;
  v[x] = 1;
}
int main() {
  cin >> n >> m;
  ans = n;
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y;
    a[x].push_back(y);
    b[x].push_back(y);
    b[y].push_back(x);
  }
  vector<int>::iterator it;
  for (int i = 1; i <= n; ++i)
    if (!v[i]) {
      ok = 0;
      k = 0;
      c.clear();
      udfs(i);
      for (it = c.begin(); it != c.end(); ++it) dfs(*it);
      if (!ok) ans--;
    }
  cout << ans;
  return 0;
}
