#include <bits/stdc++.h>
using namespace std;
const int maxn = 100500;
int n, m, ans, scc, idx;
int d[maxn], vis[maxn];
vector<int> g[maxn], e[maxn], vec;
queue<int> q;
void dfs(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  vec.push_back(u);
  for (auto v : e[u]) {
    dfs(v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    e[x].push_back(y);
    e[y].push_back(x);
    d[y]++;
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      vec.clear();
      dfs(i);
      int sum = vec.size(), sz = 0;
      for (auto v : vec)
        if (!d[v]) q.push(v), sz++;
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto v : g[u]) {
          d[v]--;
          if (!d[v]) {
            sz++;
            q.push(v);
          }
        }
      }
      if (sz == sum)
        ans += sum - 1;
      else
        ans += sum;
    }
  printf("%d\n", ans);
  return 0;
}
