#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000011, maxm = 2000011;
int tot = 0, v[maxm], son[maxm], pre[maxm], now[maxn];
void add(int a, int b) {
  pre[++tot] = now[a];
  now[a] = tot;
  son[tot] = b;
}
int fa[maxn], n, m;
bool Vis[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void init() {
  scanf("%d%d", &n, &m);
  memset(Vis, 0, sizeof(Vis));
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1, a, b; i <= m; ++i) {
    scanf("%d%d", &a, &b);
    Vis[a] = 1;
    Vis[b] = 1;
    add(a, b);
    fa[find(a)] = find(b);
  }
}
int dfn[maxn], low[maxn], Stack[maxn], stot = 0, sign = 0, ans;
bool b[maxn], B[maxn];
void tarjan(int x) {
  dfn[x] = low[x] = ++sign;
  Stack[++stot] = x;
  b[x] = 1;
  for (int p = now[x]; p; p = pre[p])
    if (!dfn[son[p]]) {
      tarjan(son[p]);
      low[x] = min(low[x], low[son[p]]);
    } else if (b[son[p]])
      low[x] = min(low[x], dfn[son[p]]);
  if (dfn[x] == low[x]) {
    bool ok = 0;
    if (Stack[stot] != x) ok = 1;
    while (Stack[stot + 1] != x) {
      if (ok) B[find(Stack[stot])] = 1;
      b[Stack[stot--]] = 0;
    }
  }
}
void work() {
  ans = 0;
  memset(B, 0, sizeof(B));
  for (int i = 1; i <= n; ++i)
    if (Vis[i]) ++ans;
  for (int i = 1; i <= n; ++i)
    if (Vis[i] && !dfn[i]) tarjan(i);
  for (int i = 1; i <= n; ++i)
    if (Vis[i] && find(i) == i && !B[i]) --ans;
  cout << ans << endl;
}
int main() {
  init();
  work();
  return 0;
}
