#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n, m, cnt, color[MAXN], cnt_color[MAXN], cycle[MAXN], visited[MAXN];
vector<int> graph[MAXN], graph2[MAXN];
void dfs(int u, int p) {
  color[u] = cnt;
  ++cnt_color[cnt];
  for (int v : graph2[u]) {
    if (v == p) continue;
    if (!color[v]) dfs(v, p);
  }
}
void dfs2(int u) {
  visited[u] = 1;
  for (int v : graph[u]) {
    if (visited[v] == 1)
      cycle[color[u]] = 1;
    else if (visited[v] == 0)
      dfs2(v);
  }
  visited[u] = 2;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0, a, b; i < m; ++i) {
    scanf("%d %d", &a, &b);
    graph[a].push_back(b);
    graph2[a].push_back(b);
    graph2[b].push_back(a);
  }
  for (int i = 1; i <= n; ++i) {
    if (!color[i]) {
      ++cnt;
      dfs(i, -1);
    }
  }
  for (int i = 1; i <= n; ++i)
    if (!visited[i]) dfs2(i);
  int ans = 0;
  for (int i = 1; i <= cnt; ++i) {
    ans += cnt_color[i] - 1 + cycle[i];
  }
  printf("%d\n", ans);
  return 0;
}
