#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-11;
vector<vector<int> > g, gr;
vector<int> num, inComponent;
bitset<(int)1e5 + 5> us;
void dfs(int v, vector<int>& order) {
  us[v] = true;
  for (int i = 0; i < (int)g[v].size(); i++)
    if (!us[g[v][i]]) dfs(g[v][i], order);
  order.push_back(v);
}
vector<int> topsort() {
  us.reset();
  vector<int> res;
  for (int i = 0; i < (int)g.size(); i++)
    if (!us[i]) dfs(i, res);
  reverse(res.begin(), res.end());
  return res;
}
void dfs2(int v, int k) {
  us[v] = true;
  num[v] = k;
  for (int i = 0; i < (int)gr[v].size(); i++)
    if (!us[gr[v][i]]) dfs2(gr[v][i], k);
}
int dfs3(int v) {
  us[v] = true;
  int res = inComponent[num[v]] == 1;
  for (int i = 0; i < (int)g[v].size(); i++)
    if (!us[g[v][i]]) res &= dfs3(g[v][i]);
  for (int i = 0; i < (int)gr[v].size(); i++)
    if (!us[gr[v][i]]) res &= dfs3(gr[v][i]);
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  g.resize(n);
  gr.resize(n);
  for (int i = 0; i < (int)m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    gr[b].push_back(a);
  }
  vector<int> order = topsort();
  us.reset();
  num.resize(n);
  int k = 0;
  for (int i = 0; i < n; i++)
    if (!us[order[i]]) dfs2(order[i], k++);
  inComponent.resize(k);
  for (int i = 0; i < (int)n; i++) inComponent[num[i]]++;
  us.reset();
  int ans = n;
  for (int i = 0; i < (int)n; i++)
    if (!us[i]) ans -= dfs3(i);
  cout << ans << endl;
  return 0;
}
