#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> g(1000000 + 2);
long long id[1000005], idx, deg[1000005], fa[1000005], tag[1000005];
long long find(long long x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(long long x, long long y) {
  x = find(x);
  y = find(y);
  fa[x] = y;
}
void dfs(long long p) {
  id[p] = -1;
  for (long long q : g[p]) {
    if (id[q] == 0) dfs(q);
  }
  id[p] = ++idx;
}
signed main() {
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) fa[i] = i;
  for (long long i = 1; i <= m; i++) {
    long long u, v;
    cin >> u >> v;
    g[u].push_back(v);
    deg[v]++;
    merge(u, v);
  }
  for (long long i = 1; i <= n; i++)
    if (deg[i] == 0 && id[i] == 0) dfs(i);
  for (long long i = 1; i <= n; i++)
    if (id[i] == 0) tag[find(i)] = 1;
  for (long long i = 1; i <= n; i++) {
    for (auto j : g[i])
      if (id[i] < id[j]) tag[find(i)] = 1;
  }
  long long ans = n;
  for (long long i = 1; i <= n; i++)
    if (find(i) == i) {
      if (tag[i] == 0) ans--;
    }
  cout << ans << endl;
}
