#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<int> adj[N];
int p[N], cycle[N], sz[N];
bool vis[N], instack[N];
int fin(int i) {
  if (p[i] != i) p[i] = fin(p[i]);
  return p[i];
}
void merge(int x, int y) {
  int px = fin(x), py = fin(y);
  if (px == py) return;
  p[px] = py;
  sz[py] += sz[px];
}
void dfs(int s) {
  if (vis[s]) return;
  vis[s] = true;
  instack[s] = true;
  for (auto it : adj[s]) {
    dfs(it);
    if (instack[it]) {
      cycle[fin(s)] = 1;
    }
  }
  instack[s] = false;
}
int main() {
  int n, m, i, ndc = 0, ans = 0, tc = 0;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) p[i] = i, sz[i] = 1;
  for (i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    adj[x - 1].push_back(y - 1);
    merge(x - 1, y - 1);
  }
  for (i = 0; i < n; i++) dfs(i);
  for (i = 0; i < n; i++) {
    if (fin(i) == i) {
      ans += sz[i] + cycle[i] - 1;
    }
  }
  printf("%d", ans);
}
