#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int n, m, u, v, color[N], f[N], sz[N], cycle[N], res;
vector<int> g[N];
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
void merge(int u, int v) {
  if ((u = find(u)) == (v = find(v))) return;
  if (sz[u] < sz[v]) swap(u, v);
  sz[u] += sz[v];
  f[v] = u;
}
void dfs(int u) {
  color[u] = 1;
  for (int x : g[u]) {
    merge(u, x);
    if (color[x] == 0)
      dfs(x);
    else if (color[x] == 1) {
      cycle[x] = 1;
    }
  }
  color[u] = 2;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = (int)(1); i <= (int)(m); ++i) {
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
  }
  for (int i = (int)(1); i <= (int)(n); ++i) f[i] = i, sz[i] = 1;
  for (int i = (int)(1); i <= (int)(n); ++i)
    if (!color[i]) {
      dfs(i);
    }
  for (int i = (int)(1); i <= (int)(n); ++i) cycle[find(i)] |= cycle[i];
  for (int i = (int)(1); i <= (int)(n); ++i)
    if (find(i) == i) {
      if (cycle[i])
        res += sz[i];
      else
        res += sz[i] - 1;
    }
  printf("%d\n", res);
}
