#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10, MOD = 1e9 + 7;
vector<vector<int> > E, F;
vector<int> D;
vector<bool> V;
int dfs(int u) {
  if (V[u]) return 0;
  V[u] = 1;
  int r = 1;
  for (int v : E[u]) r += dfs(v);
  for (int v : F[u]) r += dfs(v);
  return r;
}
int main() {
  int n, m;
  cin >> n >> m;
  E = vector<vector<int> >(n);
  F = vector<vector<int> >(n);
  V = vector<bool>(n);
  D = vector<int>(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    E[u - 1].push_back(v - 1);
    F[v - 1].push_back(u - 1);
    D[v - 1]++;
  }
  vector<int> Q;
  for (int i = 0; i < n; i++)
    if (D[i] == 0) Q.push_back(i);
  for (int q = 0; q < Q.size(); q++)
    for (int v : E[Q[q]])
      if (!--D[v]) Q.push_back(v);
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (D[i]) ans += dfs(i);
  for (int i = 0; i < n; i++)
    if (!D[i]) ans += max(0, dfs(i) - 1);
  cout << ans << '\n';
  cin >> n;
}
