#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> g[100500], f[100500];
char was[100500];
int C, cl[100500];
vector<int> a[100500];
void dfs1(int v) {
  a[C].push_back(v);
  was[v] = 1;
  for (int i = 0; i < f[v].size(); ++i) {
    int to = f[v][i];
    if (!was[to]) dfs1(to);
  }
  return;
}
bool dfs2(int v) {
  cl[v] = 1;
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (cl[to] != 0) {
      if (cl[to] == 1) return 1;
    } else {
      if (dfs2(to)) return 1;
    }
  }
  cl[v] = 2;
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int v, u;
    scanf("%d%d", &v, &u);
    g[v].push_back(u);
    f[v].push_back(u);
    f[u].push_back(v);
  }
  C = 0;
  for (int i = 1; i <= n; ++i) {
    if (!was[i]) {
      dfs1(i);
      C++;
    }
  }
  int ans = 0;
  for (int i = 0; i < C; ++i) {
    bool cycle = 0;
    for (int j = 0; j < a[i].size(); ++j) {
      int v = a[i][j];
      if (!cl[v]) {
        cycle |= dfs2(v);
      }
    }
    ans += (a[i].size() - 1);
    if (cycle) ++ans;
  }
  printf("%d\n", ans);
  return 0;
}
