#include <bits/stdc++.h>
using namespace std;
int main(void) {
  srand(time(0));
  cout << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  int n, m;
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  static vector<int> s[1 << 20];
  static vector<int> v[1 << 20];
  while (m--) {
    int a, b;
    cin >> a >> b;
    s[a].push_back(b);
    v[b].push_back(a);
  }
  vector<int> order;
  int ans = 0;
  static int was[1 << 20];
  function<void(int)> dfs1 = [&](int node) {
    was[node] = 1;
    for (auto it : s[node])
      if (!was[it]) dfs1(it);
    order.push_back(node);
  };
  int mark = 0;
  static int sz[1 << 20];
  function<void(int)> dfs2 = [&](int node) {
    was[node] = mark;
    for (auto it : v[node])
      if (!was[it]) dfs2(it);
  };
  function<int(int)> dfs = [&](int node) {
    int ok = sz[was[node]] == 1;
    was[node] = 0;
    for (auto it : v[node])
      if (was[it]) ok &= dfs(it);
    for (auto it : s[node])
      if (was[it]) ok &= dfs(it);
    return ok;
  };
  for (int i = 1; i <= n; ++i)
    if (!was[i]) dfs1(i);
  reverse(order.begin(), order.end());
  memset(was, 0, sizeof(was));
  for (auto it : order)
    if (!was[it]) ++mark, dfs2(it);
  for (int i = 1; i <= n; ++i) ++sz[was[i]];
  ans = n;
  for (int i = 1; i <= n; ++i)
    if (was[i]) ans -= dfs(i);
  cout << ans << '\n';
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}
