#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int high[MAXN], num[MAXN], comp[MAXN], comp_num, dfs_num, cnt[MAXN];
bool mark[MAXN];
vector<int> v[MAXN], g[MAXN], s;
void DFS(int n) {
  num[n] = dfs_num--;
  s.push_back(n);
  high[n] = num[n];
  for (int i = 0; i < v[n].size(); i++) {
    int x = v[n][i];
    if (num[x] == 0) {
      DFS(x);
      high[n] = max(high[n], high[x]);
    } else if (num[x] > num[n] && comp[x] == 0)
      high[n] = max(high[n], num[x]);
  }
  if (high[n] == num[n]) {
    comp_num++;
    int last = -1;
    while (last != n) {
      comp[s.back()] = comp_num;
      last = s.back();
      s.pop_back();
      cnt[comp_num]++;
    }
  }
}
void DFS2(int n) {
  if (mark[n]) return;
  s.push_back(comp[n]);
  mark[n] = true;
  for (int i = 0; i < g[n].size(); i++) DFS2(g[n][i]);
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs_num = n;
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (num[i] == 0) DFS(i);
  ans += n;
  for (int i = 0; i < n; i++)
    if (!mark[i]) {
      DFS2(i);
      bool f = false;
      for (int i = 0; i < s.size(); i++)
        if (cnt[s[i]] > 1) f = true;
      s.clear();
      if (f == false) ans--;
    }
  cout << ans << endl;
  return 0;
}
