#include <bits/stdc++.h>
using namespace std;
const long long MX = 1e5 + 9;
long long par[MX];
vector<long long> adj[MX], comp[MX];
bool st[MX], ft[MX], res;
long long FS(long long v) {
  if (par[v] < 0) return v;
  return par[v] = FS(par[v]);
}
void US(long long a, long long b) {
  a = FS(a), b = FS(b);
  if (a == b) return;
  if (par[a] > par[b]) swap(a, b);
  par[a] += par[b];
  par[b] = a;
}
void DFS(long long u) {
  st[u] = 1;
  for (long long v : adj[u]) {
    if (!st[v])
      DFS(v);
    else if (!ft[v])
      res = 1;
  }
  ft[u] = 1;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  fill(par, par + n + 1, -1);
  for (long long i = 0; i < m; i++) {
    long long u, v;
    cin >> u >> v;
    US(u, v);
    adj[u].push_back(v);
  }
  for (long long i = 1; i <= n; i++) comp[FS(i)].push_back(i);
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    res = 0;
    for (long long u : comp[i]) {
      if (!st[u]) DFS(u);
    }
    ans += (comp[i].size() - 1 + res) * (comp[i].size() > 0);
  }
  cout << ans;
  return 0;
}
