#include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 5;
int low[MX], depth[MX], timer = 0, compcnt = 0, comp[MX], compsz[MX], vi[MX];
vector<int> adj[MX], adj1[MX];
stack<int> S;
void dfs(int x) {
  low[x] = depth[x] = ++timer;
  S.push(x);
  vi[x] = 1;
  int sz_ = adj[x].size(), nxt;
  for (int j = 0; j < sz_; j++) {
    nxt = adj[x][j];
    if (depth[nxt] == -1) {
      dfs(nxt);
      low[x] = min(low[x], low[nxt]);
    } else if (vi[nxt])
      low[x] = min(low[x], depth[nxt]);
  }
  if (low[x] == depth[x]) {
    ++compcnt;
    while (1) {
      nxt = S.top();
      comp[nxt] = compcnt;
      vi[nxt] = 0;
      compsz[compcnt]++;
      S.pop();
      if (nxt == x) break;
    }
  }
}
int in[MX], par[MX];
bool vis[MX], big;
int dfs2(int x) {
  vis[x] = 1;
  if (compsz[x] > 1) big = 1;
  int ret = compsz[x];
  for (auto nxt : adj1[x]) {
    if (vis[nxt]) continue;
    ret += dfs2(nxt);
  }
  return ret;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
  }
  memset(depth, -1, sizeof depth);
  for (int i = 1; i <= n; ++i)
    if (depth[i] == -1) dfs(i);
  for (int i = 1; i <= n; ++i) {
    int x = comp[i];
    for (auto nxt : adj[i]) {
      nxt = comp[nxt];
      if (x == nxt) continue;
      adj1[x].push_back(nxt);
      adj1[nxt].push_back(x);
    }
  }
  int ans = 0;
  for (int i = 1; i <= compcnt; ++i) {
    if (vis[i]) continue;
    big = 0;
    int sz = dfs2(i);
    ans += sz - 1 + big;
  }
  cout << ans << endl;
}
