#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v1(100005), v2(100005);
vector<int> v;
int u[100005];
int a[100005];
int r;
void dfs1(int cur) {
  u[cur] = 1;
  v.push_back(cur);
  int i, x;
  for (i = 0; i < v1[cur].size(); i++) {
    x = v1[cur][i];
    if (u[x] == 0) {
      dfs1(x);
    }
  }
  for (i = 0; i < v2[cur].size(); i++) {
    x = v2[cur][i];
    if (u[x] == 0) {
      dfs1(x);
    }
  }
}
void dfs2(int cur) {
  if (r == 1) {
    return;
  }
  if (a[cur] == 2) {
    return;
  }
  if (a[cur] == 1) {
    r = 1;
    return;
  }
  a[cur] = 1;
  int i, x;
  for (i = 0; i < v1[cur].size(); i++) {
    x = v1[cur][i];
    dfs2(x);
  }
  a[cur] = 2;
}
int i, j, k, n, m, x, y, z, t, res;
int main() {
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    v1[x].push_back(y);
    v2[y].push_back(x);
  }
  for (i = 0; i < n; i++) {
    if (u[i] == 0) {
      v.clear();
      dfs1(i);
      r = 0;
      for (j = 0; j < v.size(); j++) {
        if (a[v[j]] == 0) {
          dfs2(v[j]);
        }
      }
      res += v.size();
      if (r == 0) {
        res--;
      }
    }
  }
  cout << res << endl;
  return 0;
}
