#include <bits/stdc++.h>
int edge[300000], next[300000], first[120000];
int b[120000], c[120000], d[120000], f[120000], h[120000];
int i, k, m, n, s, x, y, fx, fy;
inline int getfather(int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = getfather(f[x]);
}
inline bool dfs(int x, int y) {
  b[x] = 1, d[x] = 1;
  for (int i = first[x]; i != 0; i = next[i])
    if (!b[edge[i]]) {
      if (dfs(edge[i], x)) return true;
    } else {
      if (d[edge[i]]) return true;
    }
  d[x] = 0;
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) f[i] = i, h[i] = 1;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    edge[i] = y, next[i] = first[x], first[x] = i;
    fx = getfather(x), fy = getfather(y);
    if (fx != fy) f[fy] = fx, h[fx] = h[fx] + h[fy], h[fy] = 0;
  }
  for (i = 1; i <= n; i++)
    if (!b[i]) {
      k++;
      if (dfs(i, 0)) c[getfather(i)] = 1;
    }
  for (i = 1; i <= n; i++)
    if (h[i]) s = s + h[i] + c[i] - 1;
  printf("%d", s);
  return 0;
}
