#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n, m, ans, l;
int mark[MAXN], comp[MAXN], scc[MAXN], cyc[MAXN];
vector<int> edge[MAXN], adj[MAXN], radj[MAXN];
deque<int> dq;
void dfs(int source) {
  mark[source]++;
  ans++, comp[source] = l;
  for (auto &u : edge[source])
    if (!mark[u]) dfs(u);
}
void dfs1(int source) {
  mark[source]++;
  for (auto &u : adj[source])
    if (!mark[u]) dfs1(u);
  dq.push_back(source);
}
void rev(int source) {
  mark[source]++;
  scc[l]++;
  for (auto &u : radj[source])
    if (mark[u] == 1) rev(u);
}
int main() {
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    edge[x].push_back(y), edge[y].push_back(x);
    adj[x].push_back(y), radj[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    if (!mark[i]) ++l, dfs(i), ans--;
  fill(mark, mark + MAXN, 0);
  l = 0;
  for (int i = 1; i <= n; i++)
    if (!mark[i]) dfs1(i);
  while (dq.size()) {
    int x = dq.back();
    dq.pop_back();
    if (mark[x] < 2 && !cyc[comp[x]]) {
      ++l, rev(x);
      if (scc[l] > 1) cyc[comp[x]] = true, ans++;
    }
  }
  cout << ans;
  return 0;
}
