#include <bits/stdc++.h>
using namespace std;
vector<int> v[100001];
int c[100001];
int n;
int m;
int dsu[100001];
int F(int x) {
  if (dsu[x] < 0)
    return x;
  else {
    dsu[x] = F(dsu[x]);
    return dsu[x];
  }
}
void U(int x, int y) {
  x = F(x);
  y = F(y);
  if (x == y) return;
  if (dsu[x] < dsu[y]) swap(x, y);
  dsu[x] += dsu[y];
  dsu[y] = x;
}
bool as = false;
void topo(int q) {
  c[q] = 1;
  for (int i = 0; i < v[q].size(); ++i) {
    if (!c[v[q][i]]) {
      topo(v[q][i]);
    } else if (c[v[q][i]] == 1) {
      as = true;
    }
  }
  c[q] = -1;
}
vector<int> be[100001];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) dsu[i] = -1;
  for (int i = 1; i <= m; ++i) {
    int x, y;
    cin >> x >> y;
    U(x, y);
    v[x].push_back(y);
  }
  int cur = 0;
  for (int i = 1; i <= n; ++i) {
    be[F(i)].push_back(i);
  }
  for (int j = 1; j <= n; ++j) {
    if (!be[j].size()) continue;
    for (int i = 0; i < be[j].size(); ++i) {
      if (!c[be[j][i]]) topo(be[j][i]);
    }
    cur += (-dsu[F(be[j][0])]);
    if (!as) --cur;
    as = false;
  }
  cout << cur;
}
