#include <bits/stdc++.h>
using namespace std;
int N, M, pt, last, num, tot, ans;
int px[100010], py[100010];
int st[100010], ne[200010], go[200010];
int dfn[100010], low[100010], sta[100010], vis[100010], ss[100010],
    mark[100010];
void Add(int x, int y) {
  ne[++pt] = st[x];
  st[x] = pt;
  go[pt] = y;
}
void tarjan(int x) {
  dfn[x] = low[x] = ++tot;
  sta[++last] = x;
  vis[x] = 1;
  for (int i = st[x]; i; i = ne[i])
    if (vis[go[i]] == 0) {
      tarjan(go[i]);
      low[x] = min(low[x], low[go[i]]);
    } else if (vis[go[i]] == 1)
      low[x] = min(low[x], dfn[go[i]]);
  if (dfn[x] == low[x]) {
    ++num;
    int p;
    ss[num] = 0;
    do {
      p = sta[last];
      mark[p] = num;
      ss[num]++;
      vis[p] = 2;
      sta[last--] = 0;
    } while (p != x);
  }
}
bool floodfill(int x) {
  vis[x] = 1;
  bool flag = (ss[mark[x]] > 1);
  for (int i = st[x]; i; i = ne[i])
    if (!vis[go[i]]) {
      flag |= floodfill(go[i]);
    }
  return flag;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= M; i++) {
    scanf("%d%d", &px[i], &py[i]);
    Add(px[i], py[i]);
  }
  ans = N;
  for (int i = 1; i <= N; i++)
    if (!vis[i]) tarjan(i);
  for (int i = 1; i <= M; i++) Add(py[i], px[i]);
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= N; i++)
    if (!vis[i]) {
      ans--;
      bool flag = floodfill(i);
      ans += flag;
    }
  printf("%d\n", ans);
  return 0;
}
