#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
bool visited[MAXN], recStack[MAXN];
vector<int> adj[MAXN], kel[MAXN];
int par[MAXN];
int find(int x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}
void join(int a, int b) { par[find(a)] = find(b); }
bool dfs(int now) {
  visited[now] = 1;
  recStack[now] = true;
  for (auto nxt : adj[now]) {
    if (!visited[nxt] && dfs(nxt)) {
      return true;
    } else if (recStack[nxt]) {
      return true;
    }
  }
  recStack[now] = false;
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, u, v;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) par[i] = i;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    join(u, v);
  }
  for (int i = 1; i <= n; i++) kel[find(i)].push_back(i);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (kel[i].empty()) continue;
    bool cycle = 0;
    for (auto isi : kel[i]) {
      if (visited[isi]) continue;
      if (dfs(isi)) {
        cycle = 1;
        break;
      }
    }
    ans += kel[i].size();
    if (!cycle) ans--;
  }
  cout << ans << '\n';
  return 0;
}
