#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 3e5 + 5;
int n, m, cnt, h[N], num, ser[N], low[N], dfn[N], tot, sta[N], top, siz[N],
    flag[N];
bool vis[N];
struct Edge {
  int to, next;
} e[N];
int to[N], fa[N];
long long ans;
void add(int u, int v) {
  e[++cnt] = (Edge){v, h[u]};
  h[u] = cnt;
}
void Dfs(int u) {
  dfn[u] = low[u] = ++tot;
  vis[u] = 1;
  sta[++top] = u;
  for (int i = h[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (!dfn[v]) {
      Dfs(v);
      low[u] = min(low[u], low[v]);
    } else if (vis[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (low[u] == dfn[u]) {
    int cur;
    num++;
    do {
      siz[num]++;
      cur = sta[top--];
      vis[cur] = 0;
      to[cur] = num;
    } while (cur != u);
  }
}
int get(int u) { return (u == fa[u]) ? u : fa[u] = get(fa[u]); }
int main() {
  cin >> n >> m;
  for (register int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
  }
  for (register int i = 1; i <= n; i++)
    if (!dfn[i]) Dfs(i);
  for (register int i = 1; i <= num; i++) {
    fa[i] = i;
    if (siz[i] > 1) flag[i] = 1;
  }
  for (register int u = 1; u <= n; u++) {
    int x = to[u];
    for (int i = h[u]; i; i = e[i].next) {
      int v = e[i].to;
      int y = to[v];
      int fx = get(x), fy = get(y);
      if (fx == fy) continue;
      if (siz[fx] > 1 || siz[fy] > 1) flag[fx] = 1;
      fa[fy] = fx;
      flag[fx] += flag[fy];
    }
  }
  for (register int i = 1; i <= n; i++) {
    ans += siz[i];
    int fx = get(i);
    if (fx == i && !flag[i]) ans--;
  }
  cout << ans << endl;
}
