#include <bits/stdc++.h>
using namespace std;
int n, m, cnt[100005], mp[100005], par[100005], ans;
bool vis[100005];
vector<int> v[100005];
int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) par[i] = i;
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
    par[find(a)] = find(b);
    mp[b]++;
  }
  queue<int> q;
  for (int i = 1; i <= n; ++i) {
    cnt[find(i)]++;
    if (!mp[i]) q.push(i);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto &x : v[u])
      if (!--mp[x]) q.push(x);
  }
  for (int i = 1; i <= n; ++i)
    if (mp[i]) vis[find(i)] = 1;
  for (int i = 1; i <= n; ++i)
    if (par[i] == i) ans += cnt[i] - (vis[i] ^ 1);
  printf("%d\n", ans);
  return 0;
}
