#include <bits/stdc++.h>
using namespace std;
class SCC {
 private:
  int n, comp_sz;
  vector<vector<int> > g, gt;
  vector<int> seq, vis;
  void dfs(int u, const vector<vector<int> > &adj) {
    for (int v : adj[u]) {
      if (vis[v] == -1) {
        vis[v] = comp_sz;
        dfs(v, adj);
      }
    }
    seq.push_back(u);
  }

 public:
  SCC(int _n) {
    n = _n;
    g.assign(n, vector<int>());
    gt.assign(n, vector<int>());
  }
  void add_edge(int u, int v) {
    g[u].push_back(v);
    gt[v].push_back(u);
  }
  pair<int, vector<int> > find_SCC() {
    vis.assign(n, -1);
    comp_sz = 0;
    for (int i = 0; i < n; i++) {
      if (vis[i] == -1) {
        vis[i] = comp_sz;
        dfs(i, g);
      }
    }
    vis.assign(n, -1);
    comp_sz = 0;
    for (int i = n - 1; i >= 0; i--) {
      int u = seq[i];
      if (vis[u] == -1) {
        vis[u] = comp_sz;
        dfs(u, gt);
        comp_sz++;
      }
    }
    return {comp_sz, vis};
  }
  vector<vector<int> > get_dag() {
    map<pair<int, int>, int> mmap;
    vector<vector<int> > dag(comp_sz, vector<int>());
    for (int u = 0; u < n; u++) {
      for (int v : g[u]) {
        if (vis[u] == vis[v]) continue;
        if (!mmap.count(pair<int, int>(vis[u], vis[v]))) {
          dag[vis[u]].push_back(vis[v]);
          dag[vis[v]].push_back(vis[u]);
          mmap[pair<int, int>(vis[u], vis[v])] = 1;
        }
      }
    }
    return dag;
  }
};
vector<int> vis, comp_sz;
bool large = false;
int dfs(const vector<vector<int> > &dag, int u) {
  int sz = 0;
  if (comp_sz[u] == 1)
    sz = 1;
  else if (large == false) {
    large = true;
    sz = 1;
  }
  for (int v : dag[u]) {
    if (!vis[v]) {
      vis[v] = 1;
      sz += dfs(dag, v);
    }
  }
  return sz;
}
int main() {
  int n, m;
  cin >> n >> m;
  SCC scc(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    scc.add_edge(u, v);
  }
  int comps;
  vector<int> cmp;
  tie(comps, cmp) = scc.find_SCC();
  comp_sz.assign(comps, 0);
  for (int i = 0; i < n; i++) {
    comp_sz[cmp[i]]++;
  }
  int res = 0;
  for (int i = 0; i < comps; i++) {
    res += ((comp_sz[i] > 1) ? comp_sz[i] : 0);
  }
  vector<vector<int> > dag = scc.get_dag();
  vis.assign(comps, 0);
  for (int i = 0; i < comps; i++) {
    if (!vis[i]) {
      large = false;
      vis[i] = 1;
      int sz = dfs(dag, i);
      res += sz - 1;
    }
  }
  cout << res << endl;
  return 0;
}
