#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
bool vis[100010];
set<int> s;
vector<int> uf;
vector<int> sz;
bool cycle;
int root(int a) {
  if (uf[a] == a) return a;
  return uf[a] = root(uf[a]);
}
void join(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b) return;
  if (sz[a] > sz[b]) {
    sz[a] += sz[b];
    uf[b] = a;
  } else {
    sz[b] += sz[a];
    uf[a] = b;
  }
}
void dfs(int u) {
  s.insert(u);
  vis[u] = true;
  int res = 0;
  for (int v : g[u]) {
    if (s.find(v) != s.end()) {
      cycle = true;
    }
    if (!vis[v]) dfs(v);
  }
  s.erase(u);
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    vis[i] = false;
    sz.push_back(1);
    uf.push_back(i);
  }
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    join(a, b);
  }
  int c = 0;
  map<int, bool> ma;
  map<int, bool>::iterator it;
  for (int i = 0; i < n; i++) {
    cycle = false;
    dfs(i);
    ma[root(i)] = (ma[root(i)] || cycle);
  }
  for (it = ma.begin(); it != ma.end(); it++) {
    if (!(it->second)) n--;
  }
  cout << n;
  return 0;
}
