#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
vector<int> adj[MAXN];
vector<int> bi_adj[MAXN];
int N, M;
int lbl = 0;
int comp[MAXN];
int sz[MAXN];
bool vis[MAXN];
bool iscyc[MAXN];
int in[MAXN];
void dfs(int cur, int x) {
  vis[cur] = true;
  comp[cur] = x;
  ++sz[x];
  for (int nxt : bi_adj[cur]) {
    if (!vis[nxt]) {
      dfs(nxt, x);
    }
  }
}
void dfscyc(int cur) {
  if (iscyc[comp[cur]]) return;
  if (in[cur] == 2) {
    iscyc[comp[cur]] = true;
    return;
  } else if (in[cur] == 1) {
    return;
  }
  in[cur] = 2;
  for (int nxt : adj[cur]) {
    dfscyc(nxt);
  }
  in[cur] = 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    adj[a].push_back(b);
    bi_adj[a].push_back(b);
    bi_adj[b].push_back(a);
  }
  for (int i = 0; i < N; ++i) {
    if (!vis[i]) {
      dfs(i, lbl);
      ++lbl;
    }
  }
  for (int i = 0; i < N; ++i) {
    if (!in[i]) {
      dfscyc(i);
    }
  }
  int res = 0;
  for (int i = 0; i < lbl; ++i) {
    if (iscyc[i]) {
      res += sz[i];
    } else {
      res += sz[i] - 1;
    }
  }
  cout << res << endl;
  return 0;
}
