#include <bits/stdc++.h>
using namespace std;
const int N = 100007;
int in[N], n, m, st[N], p, visit[N], loop[N], num[N], u[N], v[N];
vector<int> child[N];
int Arr[N], sizee[N];
void initialize_dsu(int a, int b) {
  for (int i = a; i <= b; ++i) Arr[i] = i, sizee[i] = 1;
}
int root(int a) {
  if (a == Arr[a]) return a;
  return Arr[a] = root(Arr[a]);
}
bool find(int a, int b) { return (root(a) == root(b)); }
void union_set(int a, int b) {
  int u = root(a), v = root(b);
  if (find(u, v)) return;
  if (sizee[u] < sizee[v]) swap(u, v);
  Arr[v] = Arr[u], sizee[u] += sizee[v];
}
void dfs(int u) {
  visit[u] = 1;
  for (int v : child[u]) {
    if (visit[v]) continue;
    dfs(v);
  }
  st[++p] = u;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  initialize_dsu(1, n);
  for (int i = 1; i <= m; ++i) {
    cin >> u[i] >> v[i];
    child[u[i]].push_back(v[i]);
    in[v[i]] = 1;
    union_set(u[i], v[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    if (in[i] == 0) dfs(i);
  for (int i = 1; i <= n; ++i)
    if (visit[i] == 0) dfs(i);
  reverse(st + 1, st + n + 1);
  for (int i = 1; i <= n; ++i) num[st[i]] = i;
  for (int i = 1; i <= m; ++i)
    if (num[u[i]] > num[v[i]]) {
      if (loop[root(u[i])]) continue;
      loop[root(u[i])] = 1, ++ans;
    }
  set<int> s;
  for (int i = 1; i <= n; ++i) s.insert(root(st[i]));
  for (auto k : s) ans += sizee[k] - 1;
  cout << ans << endl;
  return 0;
}
