#include <bits/stdc++.h>
int N, M;
std::vector<int> G[100001];
int pre[100001], dfs_clock, scc[100001], sccno;
int S[100001], top;
int fa[100001];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int cnt[100001];
int dfs(int u) {
  int lowu = pre[u] = ++dfs_clock;
  S[top++] = u;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (!pre[v]) {
      lowu = std::min(lowu, dfs(v));
    } else if (!scc[v]) {
      lowu = std::min(lowu, pre[v]);
    }
  }
  if (lowu == pre[u]) {
    sccno++;
    int t = 0;
    for (int v = -1; v != u;) {
      v = S[--top];
      scc[v] = sccno;
      t++;
    }
    if (t > 1) cnt[find(u)] = 1;
  }
  return lowu;
}
int main() {
  int answer = 0;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i++) fa[i] = i;
  for (int i = 0; i < M; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    if (find(u) != find(v)) fa[find(u)] = find(v), answer++;
    G[u].push_back(v);
  }
  for (int i = 0; i < N; i++)
    if (!pre[i]) {
      dfs(i);
    }
  for (int i = 0; i < N; i++) answer += cnt[i];
  printf("%d\n", answer);
  return 0;
}
