#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
vector<int> child[MAXN], daddy[MAXN];
int color[MAXN], cnt[MAXN];
bitset<MAXN> b, cycle;
stack<int> stk;
int component;
void dfs(int v, int c) {
  b[v] = true;
  color[v] = c;
  for (auto i : child[v])
    if (!b[i]) dfs(i, c);
  for (auto i : daddy[v])
    if (!b[i]) dfs(i, c);
}
void dfs1(int v) {
  b[v] = true;
  for (auto i : child[v])
    if (!b[i]) dfs1(i);
  stk.push(v);
}
void dfs2(int v, int color) {
  b[v] = true;
  ++cnt[color];
  for (auto i : daddy[v])
    if (!b[i]) dfs2(i, color);
}
int main() {
  int n, m, a1, a2;
  cin >> n >> m;
  while (m--) {
    cin >> a1 >> a2;
    child[a1].push_back(a2);
    daddy[a2].push_back(a1);
  }
  int c = 0;
  for (int i = 1; i <= n; ++i)
    if (!b[i]) {
      ++component;
      dfs(i, c++);
    }
  for (int i = 1; i <= n; ++i) b[i] = false;
  for (int i = 1; i <= n; ++i)
    if (!b[i]) dfs1(i);
  for (int i = 1; i <= n; ++i) b[i] = false;
  int k = 0;
  while (!stk.empty()) {
    if (!b[stk.top()]) dfs2(stk.top(), k++);
    if (cnt[k - 1] > 1) cycle[color[stk.top()]] = true;
    stk.pop();
  }
  int res = n - component;
  for (int i = 0; i < c; ++i)
    if (cycle[i]) ++res;
  cout << res;
  return 0;
}
