#include <bits/stdc++.h>
using namespace std;
struct Union {
  int par[100001], val[100001], cnt[100001], loop[100001];
  Union() {
    for (int j = 0; j < 100001; j++)
      par[j] = j, val[j] = 0, cnt[j] = 1, loop[j] = 0;
  }
  int parent(int x) {
    if (x == par[x]) return x;
    return par[x] = parent(par[x]);
  }
  void add(int x, int y) {
    int p1 = parent(x);
    int p2 = parent(y);
    if (p1 == p2) return;
    if (cnt[p1] > cnt[p2]) {
      par[p2] = p1;
      cnt[p1] += cnt[p2];
    } else {
      par[p1] = p2;
      cnt[p2] += cnt[p1];
    }
  }
  void update(int x, int v, int l) {
    int p = parent(x);
    val[p] = v;
    loop[p] = l;
  }
  pair<int, int> value(int x) {
    return make_pair(val[parent(x)], loop[parent(x)]);
  }
};
Union u;
vector<int> g[100001];
int mark[100001] = {0}, dfsNo[100001] = {0};
set<int> otherTree[100001];
void dfs(int src, int par, const int num, int& cycle, vector<int>& nodes) {
  if (mark[src] == 1) {
    cycle = 1;
    return;
  }
  if (mark[src]) {
    if (dfsNo[src] == dfsNo[par])
      return;
    else
      otherTree[num].insert(dfsNo[src]);
    return;
  }
  nodes.push_back(src);
  dfsNo[src] = num;
  mark[src] = 1;
  for (auto i : g[src]) dfs(i, src, num, cycle, nodes);
  mark[src] = 2;
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int j = 0; j < m; j++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
  }
  int num = 1;
  for (int j = 1; j <= n; j++) {
    int cyc = 0;
    if (!mark[j]) {
      vector<int> nodes;
      dfs(j, 0, num, cyc, nodes);
      int cnts = (cyc == 0) ? (int)nodes.size() - 1 : (int)nodes.size();
      u.update(num, cnts, cyc);
      for (auto it : otherTree[num]) {
        if (u.parent(it) == u.parent(num)) continue;
        cyc = u.value(num).second, cnts = u.value(num).first;
        if (cyc) {
          int l = u.value(it).second, v = u.value(it).first;
          u.add(it, num);
          if (l)
            u.update(it, v + cnts, 1);
          else
            u.update(it, v + cnts + 1, 1);
        } else {
          int l = u.value(it).second, v = u.value(it).first;
          u.add(num, it);
          if (l)
            u.update(it, v + cnts + 1, 1);
          else
            u.update(it, v + cnts + 1, 0);
        }
      }
      num++;
    }
  }
  int ans = 0;
  set<int> s;
  for (int j = 1; j < num; j++) {
    if (s.count(u.parent(j))) continue;
    s.insert(u.parent(j));
    ans += u.value(j).first;
  }
  cout << ans << endl;
  return 0;
}
