#include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 10;
vector<int> comp[MX], adj[MX], nadj[MX], badj[MX];
int scc[MX];
bool mrk[MX];
int t = 1;
stack<int> s;
void dfs1(int v) {
  mrk[v] = 1;
  comp[t].push_back(v);
  for (int u : nadj[v]) {
    if (!mrk[u]) dfs1(u);
  }
}
void dfs(int v) {
  mrk[v] = 1;
  for (int u : adj[v]) {
    if (!mrk[u]) dfs(u);
  }
  s.push(v);
}
void bdfs(int v) {
  mrk[v] = 1;
  for (int u : badj[v]) {
    if (!mrk[u]) bdfs(u);
  }
}
void composition(int k) {
  for (int u : comp[k]) {
    mrk[u] = 0;
  }
  for (int u : comp[k]) {
    if (!mrk[u]) {
      dfs(u);
    }
  }
  for (int u : comp[k]) {
    mrk[u] = 0;
  }
  while (s.size()) {
    int u = s.top();
    s.pop();
    if (!mrk[u]) {
      bdfs(u);
      scc[k]++;
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    nadj[a].push_back(b);
    nadj[b].push_back(a);
    adj[a].push_back(b);
    badj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!mrk[i]) {
      dfs1(i);
      t++;
    }
  }
  fill(mrk, mrk + n, 0);
  for (int i = 1; i < t; i++) {
    composition(i);
  }
  int ans = n;
  for (int i = 1; i < t; i++) {
    if (scc[i] == comp[i].size()) ans--;
  }
  cout << ans << endl;
}
