#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
void dfs(int v, vector<vector<int> > &g, vector<int> &used, vector<int> &vs) {
  used[v] = 1;
  for (__typeof((g[v]).begin()) e = (g[v]).begin(); e != (g[v]).end(); ++e)
    if (!used[*e]) dfs(*e, g, used, vs);
  vs.push_back(v);
}
void rdfs(int v, vector<vector<int> > &rg, vector<int> &used, vector<int> &rd) {
  used[v] = 1;
  for (__typeof((rg[v]).begin()) e = (rg[v]).begin(); e != (rg[v]).end(); ++e)
    if (!used[*e]) rdfs(*e, rg, used, rd);
  rd.push_back(v);
}
void scc(vector<vector<int> > &g, vector<vector<int> > &gscc, UnionFind &uf,
         vector<pair<int, int> > &cnt, vector<int> &loop) {
  int V = g.size();
  vector<int> used(V, 0);
  vector<int> vs;
  for (int i = 0; i < (int)(V); i++)
    if (!used[i]) dfs(i, g, used, vs);
  used = vector<int>(V, 0);
  vector<vector<int> > rg(V);
  for (int i = 0; i < (int)(V); i++)
    for (__typeof((g[i]).begin()) e = (g[i]).begin(); e != (g[i]).end(); ++e)
      rg[*e].push_back(i);
  for (int i = (int)V - 1; i >= 0; i--)
    if (!used[vs[i]]) {
      vector<int> rd;
      rdfs(vs[i], rg, used, rd);
      gscc.push_back(rd);
    }
  for (int i = 0; i < (int)(gscc.size()); i++) {
    int root = uf.root(gscc[i][0]);
    if (gscc[i].size() != 1) {
      loop[root] = 1;
    }
    cnt[root].first++;
    cnt[root].second += (int)gscc[i].size();
  }
}
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > g(n);
  vector<vector<int> > gg(n);
  vector<vector<int> > rgg(n);
  UnionFind uf(n);
  for (int i = 0; i < (int)(m); i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
    gg[x].push_back(y);
    rgg[y].push_back(x);
    uf.unionSet(x, y);
  }
  vector<vector<int> > gscc;
  vector<pair<int, int> > cnt(n, pair<int, int>(0, 0));
  vector<int> loop(n, 0);
  scc(g, gscc, uf, cnt, loop);
  int ans = 0;
  for (int i = 0; i < (int)(n); i++) {
    if (uf.root(i) != i) continue;
    if (cnt[i].first == 1 && cnt[i].second == 1) continue;
    ans += cnt[i].second - 1;
    if (loop[i]) ans++;
  }
  cout << ans << endl;
  return 0;
}
