#include <bits/stdc++.h>
using namespace std;
int n, m, tot, ans, Head[100000 + 5], q[100000 + 5], T[100000 + 5],
    Deg[100000 + 5];
bool Flag[100000 + 5];
struct Edge {
  int next, node;
} h[100000 + 5 << 1];
inline void addedge(int u, int v) {
  h[++tot].next = Head[u], Head[u] = tot;
  h[tot].node = v;
}
inline void dfs(int z) {
  if (Flag[z]) return;
  Flag[z] = 1;
  q[++q[0]] = z;
  for (int i = Head[z]; i; i = h[i].next) {
    int d = h[i].node;
    dfs(d);
  }
}
inline bool T_Sort() {
  int l = 1, r = 0;
  for (int i = 1; i <= q[0]; i++)
    if (!Deg[q[i]]) T[++r] = q[i];
  while (l <= r) {
    int z = T[l++];
    for (int i = Head[z]; i; i = h[i].next) {
      if (i + 1 & 1) continue;
      int d = h[i].node;
      Deg[d]--;
      if (!Deg[d]) T[++r] = d;
    }
  }
  return r == q[0];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i <= m; i++) {
    scanf("%d%d", &u, &v);
    addedge(u, v), addedge(v, u);
    Deg[v]++;
  }
  for (int i = 1; i <= n; i++) {
    if (Flag[i]) continue;
    q[0] = 0;
    dfs(i);
    ans += T_Sort() ? q[0] - 1 : q[0];
  }
  printf("%d\n", ans);
  return 0;
}
