#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector<int> e[maxn], re[maxn], List;
int n, m, id[maxn], tt, cnt[maxn], ans;
bool vis[maxn];
void input() {
  scanf("%d%d", &n, &m);
  for (int u, v, i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    e[u].push_back(v);
    re[v].push_back(u);
  }
}
void dfs(int u) {
  vis[u] = 1;
  for (vector<int>::iterator it = e[u].begin(); it != e[u].end(); it++) {
    if (!vis[*it]) {
      dfs(*it);
    }
  }
  List.push_back(u);
}
void rdfs(int u, int k) {
  id[u] = k;
  cnt[k]++;
  for (vector<int>::iterator it = re[u].begin(); it != re[u].end(); it++) {
    if (!id[*it]) {
      rdfs(*it, k);
    }
  }
}
void scc() {
  tt = 0;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  reverse(List.begin(), List.end());
  for (vector<int>::iterator it = List.begin(); it != List.end(); it++)
    if (!id[*it]) {
      tt++;
      rdfs(*it, tt);
    }
}
void bfs(int u) {
  queue<int> q;
  while (!q.empty()) q.pop();
  q.push(u);
  vis[u] = 1;
  int c = 0, x = 0;
  bool ok = 0;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    if (cnt[id[u]]) {
      c += cnt[id[u]] - 1;
      x++;
      if (cnt[id[u]] > 1) ok = 1;
      cnt[id[u]] = 0;
    }
    for (vector<int>::iterator it = e[u].begin(); it != e[u].end(); it++) {
      if (!vis[*it]) {
        vis[*it] = 1;
        q.push(*it);
      }
    }
    for (vector<int>::iterator it = re[u].begin(); it != re[u].end(); it++) {
      if (!vis[*it]) {
        vis[*it] = 1;
        q.push(*it);
      }
    }
  }
  ans += c + x - 1;
  if (ok) ans++;
}
void solve() {
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      bfs(i);
    }
  printf("%d", ans);
}
int main() {
  input();
  scc();
  solve();
}
