#include <bits/stdc++.h>
const long long N = 1000006;
using namespace std;
const long long MOD = 1000000007LL;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
template <typename T>
T power(T x, T y, long long m = MOD) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL) ans = (ans * x) % m;
    y >>= 1LL;
    x = (x * x) % m;
  }
  return ans % m;
}
vector<vector<long long> > g;
vector<vector<long long> > ac;
long long vis[N], vis2[N], in[N];
vector<long long> lt;
bool f;
vector<long long> topo;
void dfs(long long n) {
  if (vis[n]) return;
  lt.emplace_back(n);
  vis[n] = 1;
  for (auto i : g[n])
    if (!vis[i]) dfs(i);
}
void ch(long long n) {
  vis2[n] = 1;
  for (auto i : ac[n]) {
    if (!vis2[i]) ch(i);
  }
  topo.emplace_back(n);
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  g.resize(n);
  ac.resize(n);
  for (long long(i) = (0); i < (m); i++) {
    long long a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
    ac[a].emplace_back(b);
  }
  long long ans = 0;
  for (long long(i) = (0); i < (n); i++) {
    if (vis[i]) continue;
    lt.clear();
    f = 0;
    topo.clear();
    dfs(i);
    for (auto ver : lt) ch(ver);
    set<long long> s;
    for (auto x : topo) {
      if (f) break;
      for (auto u : ac[x]) {
        if (f) break;
        if (s.find(u) == s.end()) {
          f = 1;
          break;
        }
      }
      s.insert(x);
    }
    if (f)
      ans += (long long)lt.size();
    else
      ans += (long long)lt.size() - 1;
  }
  cout << ans;
}
