#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
  if (x < y) x = y;
}
struct UnionFind {
  vector<int> data;
  void init(int n) { data.assign(n, -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)]; }
};
bool topologicalSortForCC(const vector<vector<int> > &g, const vector<int> &cc,
                          vector<int> &ord, vector<int> &deg) {
  for (__typeof((cc).begin()) i = ((cc).begin()); i != (cc).end(); ++i)
    deg[*i] = 0;
  for (__typeof((cc).begin()) i = ((cc).begin()); i != (cc).end(); ++i)
    for (__typeof((g[*i]).begin()) j = ((g[*i]).begin()); j != (g[*i]).end();
         ++j)
      deg[*j]++;
  ord.clear();
  int t = 0;
  for (__typeof((cc).begin()) i = ((cc).begin()); i != (cc).end(); ++i)
    if (deg[*i] == 0) ord.push_back(*i);
  for (int h = 0; h < (int)ord.size(); h++) {
    int v = ord[h];
    for (__typeof((g[v]).begin()) j = ((g[v]).begin()); j != (g[v]).end(); ++j)
      if (--deg[*j] == 0) ord.push_back(*j);
  }
  return ord.size() == cc.size();
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  UnionFind uf;
  uf.init(n);
  vector<vector<int> > g(n);
  for (int(i) = 0; (i) < (int)(m); ++(i)) {
    int a, b;
    scanf("%d%d", &a, &b), --a, --b;
    uf.unionSet(a, b);
    g[a].push_back(b);
  }
  vector<vector<int> > ccs(n);
  for (int(i) = 0; (i) < (int)(n); ++(i)) ccs[uf.root(i)].push_back(i);
  int ans = 0;
  {
    vector<int> ord, deg(n);
    for (int(i) = 0; (i) < (int)(n); ++(i))
      if (uf.root(i) == i) {
        bool ok = topologicalSortForCC(g, ccs[i], ord, deg);
        ans += (int)ccs[i].size() - ok;
      }
  }
  printf("%d\n", ans);
  return 0;
}
