#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 9;
const long long mod = 1e9 + 7;
vector<int> g[maxn], comp[maxn];
int par[maxn], vis[maxn];
bool f;
int root(int v) { return ((par[v] + 1) ? par[v] = root(par[v]) : v); }
void merge(int v, int u) {
  v = root(v);
  u = root(u);
  if (v == u) return;
  par[v] = u;
}
void dfs(int v) {
  vis[v] = 1;
  for (auto u : g[v]) {
    if (vis[u] == 1) f = true;
    if (vis[u] == 0) dfs(u);
  }
  vis[v] = 2;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(par, -1, sizeof par);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    --v;
    --u;
    merge(v, u);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; i++) comp[root(i)].push_back(i);
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (comp[i].size() == 0) continue;
    f = false;
    for (auto u : comp[i])
      if (!vis[u]) dfs(u);
    res += comp[i].size() - ((f) ? 0 : 1);
  }
  cout << res << "\n";
}
