#include <bits/stdc++.h>
const int N = 1e5 + 9;
using namespace std;
int mark[N], id[N], cyc[N], n, m, a, b;
vector<int> v[N], d[N];
bool finished[N];
void dfs(int cur, int c) {
  id[cur] = c;
  for (int u : v[cur])
    if (!id[u]) dfs(u, c);
}
void cycle(int cur) {
  mark[cur] = 1;
  for (int u : d[cur])
    if (mark[u]) {
      if (!finished[u]) cyc[id[cur]] = true;
    } else
      cycle(u);
  finished[cur] = true;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a >> b, a--, b--, v[a].push_back(b), d[a].push_back(b),
        v[b].push_back(a);
  int ans = n, comp = 0;
  for (int i = 0; i < n; i++)
    if (!id[i]) dfs(i, ++comp), ans--;
  for (int i = 0; i < n; i++)
    if (!mark[i]) cycle(i);
  for (int i = 1; i <= comp; i++)
    if (cyc[i]) ans++;
  cout << ans;
}
