#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 9;
long long n, m, mo, cnt, ans;
vector<long long> v[N], v2[N];
vector<long long> o;
long long flag[N], mark[N], sz[N];
void dfs1(long long u) {
  if (flag[u]++) return;
  for (auto it : v[u]) dfs1(it);
  o.push_back(u);
}
void dfs2(long long u, long long cnt) {
  if (mark[u]) return;
  mark[u] = cnt;
  sz[cnt]++;
  for (auto it : v[u]) dfs2(it, cnt);
}
long long dfs3(long long u) {
  if (flag[u]++) return 1;
  long long res = 1;
  if (sz[mark[u]] > 1) res = 0;
  for (auto it : v2[u])
    if (!dfs3(it)) res = 0;
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < (m); i++) {
    long long u, w;
    cin >> u >> w;
    u--;
    w--;
    v[u].push_back(w);
    v2[u].push_back(w);
    v2[w].push_back(u);
  }
  for (long long i = 0; i < (n); i++) dfs1(i);
  fill(flag, flag + N, 0);
  for (auto it : o)
    if (!flag[it]) dfs2(it, ++cnt);
  fill(flag, flag + N, 0);
  ans = n;
  for (long long i = 0; i < (n); i++)
    if (!flag[i]) {
      long long res = dfs3(i);
      if (res) ans--;
    }
  cout << ans << endl;
}
