#include <bits/stdc++.h>
using namespace std;
int u[100100], v[100100];
int root[100100], cnt[100100], ord[100100];
int find_root(int u) { return u == root[u] ? u : root[u] = find_root(root[u]); }
void merge(int u, int v) {
  u = find_root(u);
  v = find_root(v);
  if (u != v) root[v] = u;
}
vector<vector<int> > adj[100100];
int indeg[100100];
int q[100100], qf, qb;
int calc(vector<vector<int> > adj, int n) {
  memset(indeg, 0, sizeof(int) * n);
  for (int i = 0; i < n; i++) {
    for (int j = adj[i].size(); j--;) {
      indeg[adj[i][j]]++;
    }
  }
  qf = qb = 0;
  for (int i = 0; i < n; i++)
    if (!indeg[i]) q[qb++] = i;
  while (qf < qb) {
    int u = q[qf++];
    for (int i = adj[u].size(); i--;) {
      int v = adj[u][i];
      if (!(--indeg[v])) q[qb++] = v;
    }
  }
  return qb == n ? n - 1 : n;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) root[i] = i;
  for (int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    merge(u[i], v[i]);
  }
  for (int i = 1; i <= n; i++) {
    ord[i] = cnt[find_root(i)]++;
    adj[find_root(i)].push_back(vector<int>(0));
  }
  for (int i = 0; i < m; i++) {
    adj[find_root(u[i])][ord[u[i]]].push_back(ord[v[i]]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (find_root(i) == i) ans += calc(adj[i], cnt[i]);
  }
  cout << ans << endl;
  return 0;
}
