#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, nxt;
} e[100005];
int head[100005], cnte, n, m;
void adde(int u, int v) {
  e[++cnte] = (edge){v, head[u]};
  head[u] = cnte;
}
int fa[100005];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
  if ((x = find(x)) ^ (y = find(y))) fa[x] = y;
}
queue<int> q;
int vis[100005], in[100005];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    adde(u, v);
    merge(u, v);
    in[v]++;
  }
  int ans = n;
  for (int i = 1; i <= n; i++)
    fa[i] = find(i), ans -= vis[fa[i]] == 0, vis[fa[i]] = 1;
  for (int i = 1; i <= n; i++)
    if (!in[i]) q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      in[v]--;
      if (!in[v]) q.push(v);
    }
  }
  for (int i = 1; i <= n; i++)
    if (in[i]) ans += vis[fa[i]], vis[fa[i]] = 0;
  return printf("%d\n", ans), 0;
}
