#include <bits/stdc++.h>
using namespace std;
const int inf = int(1e9);
const double eps = 1e-9;
const double pi = 4 * atan(double(1));
const int N = int(1e5) + 100;
bool cyc;
int sz;
int use[N];
bool used[N];
int lst[N];
vector<int> g[N], g2[N];
void dfs1(int v) {
  used[v] = true;
  lst[sz++] = v;
  for (int i = 0; i < int((g[v]).size()); ++i) {
    if (!used[g[v][i]]) {
      dfs1(g[v][i]);
    }
  }
}
void dfs2(int v) {
  use[v] = 1;
  for (int i = 0; i < int((g2[v]).size()); ++i) {
    if (use[g2[v][i]] == 0) {
      dfs2(g2[v][i]);
    } else if (use[g2[v][i]] == 1) {
      cyc = true;
    }
  }
  use[v] = 2;
}
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);
    g2[a].push_back(b);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      sz = 0;
      dfs1(i);
      cyc = false;
      for (int j = 0; j < sz; ++j) {
        if (use[lst[j]] == 0) {
          dfs2(lst[j]);
        }
      }
      if (cyc) {
        ans += sz;
      } else {
        ans += sz - 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
