#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1000000000")
using namespace std;
const int inf = (int)1e9 + 7;
const long double eps = 1e-18;
const int M = 1962;
const int N = 100005;
const int P = 137;
const int K = 11;
vector<int> g[N], gr[N];
bool u[N], ub[N], uu[N];
vector<int> comp;
void dfs(int v) {
  u[v] = 1;
  comp.push_back(v);
  for (int i = 0; i < (int)g[v].size(); ++i)
    if (!u[g[v][i]]) dfs(g[v][i]);
}
bool check(int v) {
  ub[v] = 1;
  uu[v] = 1;
  for (int i = 0; i < (int)gr[v].size(); ++i) {
    if (uu[gr[v][i]]) return 1;
    if (!ub[gr[v][i]] && check(gr[v][i])) return 1;
  }
  uu[v] = 0;
  return 0;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
    gr[a].push_back(b);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (!u[i]) {
      dfs(i);
      ans += (int)comp.size() - 1;
      for (int j = 0; j < (int)comp.size(); ++j)
        if (!ub[comp[j]] && check(comp[j])) {
          ++ans;
          break;
        }
      comp.clear();
    }
  printf("%d", ans);
  return 0;
}
