#include <bits/stdc++.h>
using namespace std;
vector<int> a[300007], v, vv, o[300007];
int vis[300007], mark[300007], b;
int n, m;
void FindC(int x) {
  vis[x] = 1;
  for (int i = 0; i < a[x].size(); i++) {
    if (vis[a[x][i]] == 1) b = true;
    if (!vis[a[x][i]]) FindC(a[x][i]);
  }
  vis[x] = 2;
  vv.push_back(x);
}
void FindM(int x) {
  mark[x] = true;
  for (int i = 0; i < o[x].size(); i++)
    if (!mark[o[x][i]]) FindM(o[x][i]);
  v.push_back(x);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    a[x].push_back(y);
    o[y].push_back(x);
    o[x].push_back(y);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      b = false;
      FindM(i);
      for (int x = 0; x < v.size(); x++)
        if (!vis[v[x]]) FindC(v[x]);
      if (b) cnt++;
      cnt += vv.size() - 1;
      vv.clear();
      v.clear();
    }
  cout << cnt << endl;
}
