#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
const int mod = 1e9 + 7;
const int inf = (1 << 30);
vector<int> v[maxn][5], tpl;
int ted[maxn], com[maxn], tmp = 1;
bool visited[maxn];
bool dfs(int u, int d) {
  visited[u] = 1;
  bool rz = (ted[com[u]] == 1);
  if (d == 1) {
    com[u] = tmp;
    ted[tmp]++;
  }
  for (auto w : v[u][d]) {
    if (!visited[w]) {
      rz &= dfs(w, d);
    }
  }
  if (d == 0) tpl.push_back(u);
  return rz;
}
bool dfs1(int u) {
  visited[u] = 1;
  bool rz = (ted[com[u]] == 1);
  for (auto w : v[u][0])
    if (!visited[w]) rz &= dfs1(w);
  for (auto w : v[u][1])
    if (!visited[w]) rz &= dfs1(w);
  return rz;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    v[a][0].push_back(b);
    v[b][1].push_back(a);
  }
  for (int i = 1; i <= n; i++)
    if (!visited[i]) dfs(i, 0);
  memset(visited, 0, sizeof visited);
  for (int i = n - 1; i >= 0; i--) {
    int u = tpl[i];
    if (!visited[u]) {
      dfs(u, 1);
      tmp++;
    }
  }
  memset(visited, 0, sizeof visited);
  long long ans = n;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      bool k = dfs1(i);
      ans -= k;
    }
  }
  cout << ans << endl;
}
