#include <bits/stdc++.h>
int e[2][100010LL], star[100010LL] = {0}, tote = 0;
inline void AddEdge(int u, int v) {
  tote++, e[0][tote] = v, e[1][tote] = star[u], star[u] = tote;
}
int fa[100010LL] = {0};
inline int GetAnc(int u) { return fa[u] ? fa[u] = GetAnc(fa[u]) : u; }
inline void Union(int u, int v) {
  u = GetAnc(u), v = GetAnc(v);
  if (u != v) fa[u] = v;
}
int ind[100010LL] = {0}, poi[100010LL];
inline bool cmp(const int& a, const int& b) {
  return fa[a] == fa[b] ? ind[a] < ind[b] : fa[a] < fa[b];
}
int sta[100010LL], top;
inline bool Circle(int L, int R) {
  int i, u, p;
  top = 0;
  for (i = L; i <= R; i++)
    if (ind[poi[i]] == 0)
      sta[++top] = poi[i];
    else
      break;
  while (top) {
    u = sta[top--];
    for (p = star[u]; p; p = e[1][p])
      if ((--ind[e[0][p]]) == 0) sta[++top] = e[0][p];
  }
  for (i = R; i >= L; i--)
    if (ind[poi[i]] > 0) return true;
  return false;
}
int n, m;
int main() {
  int i, u, v, ans, last;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    AddEdge(u, v), Union(u, v), ind[v]++;
  }
  for (i = 1; i <= n; i++) GetAnc(i), poi[i] = i;
  for (i = 1; i <= n; i++)
    if (!fa[i]) fa[i] = i;
  std::sort(poi + 1, poi + n + 1, cmp);
  ans = 0, last = 1;
  poi[n + 1] = n + 1, fa[n + 1] = -1;
  for (i = 1; i <= n; i++)
    if (fa[poi[i]] != fa[poi[i + 1]]) {
      if (Circle(last, i))
        ans += i - last + 1;
      else
        ans += i - last;
      last = i + 1;
    }
  printf("%d\n", ans);
  return 0;
}
