#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int N, M;
vector<int> adj[MAXN];
int par[MAXN], indeg[MAXN];
bool hascyc[MAXN];
int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
int main() {
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
  }
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    par[i] = i;
  }
  while (M--) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    par[find(a)] = find(b);
    indeg[b]++;
  }
  stack<int> nodeg;
  for (int i = 1; i <= N; i++) {
    if (!indeg[i]) {
      nodeg.push(i);
    }
  }
  while (!nodeg.empty()) {
    int x = nodeg.top();
    nodeg.pop();
    for (int t : adj[x]) {
      if (!--indeg[t]) {
        nodeg.push(t);
      }
    }
  }
  int ans = N;
  for (int i = 1; i <= N; i++) {
    if (indeg[i]) {
      hascyc[find(i)] = true;
    }
  }
  for (int i = 1; i <= N; i++) {
    ans -= (par[i] == i && !hascyc[i]);
  }
  cout << ans << endl;
}
