#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, ans, c[N], ctot, ind[N], q[N], ql, qr;
int head[N], nxt[N], to[N], cnt = 1;
vector<int> wcc[N], g[N];
void add(int u, int v) {
  nxt[++cnt] = head[u];
  head[u] = cnt;
  to[cnt] = v;
}
void findwcc(int u) {
  wcc[c[u]].push_back(u);
  for (auto v : g[u]) {
    if (c[v]) continue;
    c[v] = c[u];
    findwcc(v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    ++ind[v];
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    if (!c[i]) {
      c[i] = ++ctot;
      findwcc(i);
    }
  }
  for (int i = 1; i <= ctot; ++i) {
    ql = 1;
    qr = 0;
    vector<int> a;
    for (auto u : wcc[i]) {
      if (ind[u] == 0) {
        q[++qr] = u;
      }
    }
    while (ql <= qr) {
      int u = q[ql++];
      for (int j = head[u]; j; j = nxt[j]) {
        int v = to[j];
        if (--ind[v] == 0) q[++qr] = v;
      }
    }
    if (qr == wcc[i].size())
      ans += qr - 1;
    else
      ans += wcc[i].size();
  }
  cout << ans;
  return 0;
}
