#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
const int maxm = 100000 + 10;
struct edge {
  int x, y, next;
};
edge gr[maxm], gd[maxm];
int tot, n, m, fir[maxn], fid[maxn];
int L[maxn], col[maxn], totcol;
int fa[maxn], sumc[maxn];
bool mark[maxn];
void dfs_d(int u) {
  mark[u] = true;
  for (int t = fir[u]; t > 0; t = gr[t].next)
    if (!mark[gr[t].y]) {
      dfs_d(gr[t].y);
    }
  L[++L[0]] = u;
}
void dfs_c(int u, int c) {
  col[u] = c;
  for (int t = fid[u]; t > 0; t = gd[t].next)
    if (col[gd[t].y] == 0) {
      dfs_c(gd[t].y, c);
    }
}
void add(int x, int y) {
  ++tot;
  gr[tot].x = x, gr[tot].y = y, gr[tot].next = fir[x];
  gd[tot].x = y, gd[tot].y = x, gd[tot].next = fid[y];
  fir[x] = tot;
  fid[y] = tot;
}
int find(int u) {
  if (fa[u] == u) return u;
  fa[u] = find(fa[u]);
  return fa[u];
}
int main() {
  int x, y;
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(fir, 0, sizeof(fir));
    memset(fid, 0, sizeof(fid));
    tot = 0;
    for (int i = 1; i <= n; i++) fa[i] = i;
    for (int i = 0; i != m; i++) {
      scanf("%d%d", &x, &y);
      add(x, y);
      if (find(x) != find(y)) {
        fa[fa[x]] = fa[y];
      }
    }
    memset(mark, 0, sizeof(mark));
    L[0] = 0;
    for (int i = 1; i <= n; i++) {
      if (!mark[i]) dfs_d(i);
    }
    memset(col, 0, sizeof(col));
    totcol = 0;
    for (int i = n; i >= 1; i--) {
      if (col[L[i]] == 0) {
        dfs_c(L[i], ++totcol);
      }
    }
    memset(sumc, 0, sizeof(sumc));
    for (int i = 1; i <= n; i++) ++sumc[col[i]];
    int ans = n;
    memset(mark, 0, sizeof(mark));
    for (int i = 1; i <= n; i++) {
      if (sumc[col[i]] > 1) mark[find(i)] = true;
    }
    for (int i = 1; i <= n; i++) {
      if (find(i) == i) ans = ans - 1 + mark[i];
    }
    printf("%d\n", ans);
  }
  return 0;
}
