#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
const long long int mod = 998244353;
const int inf = 1e7 + 100;
bool mark[N];
vector<int> v, to, adj[N], g[N];
int ind[N];
void dfs(int u) {
  v.push_back(u);
  mark[u] = true;
  for (auto x : adj[u]) {
    if (!mark[x]) dfs(x);
  }
}
void dfs2(int u) {
  mark[u] = true;
  for (auto x : g[u]) {
    if (!mark[x]) dfs2(x);
  }
  to.push_back(u);
}
inline int solve() {
  int n = v.size();
  for (auto u : v) mark[u] = false;
  for (auto u : v) {
    if (!mark[u]) dfs2(u);
  }
  for (int i = 0; i < n; i++) {
    ind[to[i]] = i;
  }
  bool fl = true;
  for (auto u : v) {
    for (auto x : g[u]) {
      if (ind[u] < ind[x]) fl = false;
    }
  }
  to.clear();
  v.clear();
  if (fl)
    return n - 1;
  else
    return n;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!mark[i]) {
      dfs(i);
      ans += solve();
    }
  }
  cout << ans << endl;
}
