#include <bits/stdc++.h>
using namespace std;
const int N = 4 * 1e5 + 2;
const int MOD = 1000000007;
int n, m;
vector<unordered_set<int>> vec;
vector<unordered_set<int>> vec_rev;
vector<bool> visited;
stack<int> stk;
vector<unordered_set<int>> SCC;
vector<int> dsu;
unordered_set<int> deleted;
int getParent(int node) {
  if (dsu[node] == node) return node;
  return dsu[node] = getParent(dsu[node]);
}
void makeParent(int a, int b) {
  a = getParent(a);
  b = getParent(b);
  dsu[a] = b;
}
void dfs1(int node) {
  if (visited[node]) return;
  visited[node] = true;
  for (int next : vec[node]) {
    dfs1(next);
  }
  stk.push(node);
}
void dfs2(int node) {
  if (visited[node]) return;
  visited[node] = true;
  SCC.back().insert(node);
  for (int next : vec_rev[node]) {
    dfs2(next);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vec.resize(n + 2);
  vec_rev.resize(n + 2);
  int a, b;
  dsu.resize(n + 2);
  for (int i = 0; i <= n; i++) dsu[i] = i;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    vec[a].insert(b);
    vec_rev[b].insert(a);
  }
  visited.resize(n + 2, false);
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs1(i);
    }
  }
  visited.clear();
  visited.resize(n + 2, false);
  while (stk.size()) {
    int node = stk.top();
    stk.pop();
    if (!visited[node]) {
      SCC.emplace_back();
      dfs2(node);
    }
  }
  visited.clear();
  visited.resize(n + 2, false);
  for (int i = 1; i <= n; i++) {
    for (int next : vec[i]) {
      makeParent(i, next);
    }
  }
  unordered_set<int> par_SCC;
  for (auto it : SCC) {
    if (it.size() != 1) par_SCC.insert(getParent(*it.begin()));
  }
  for (int i = 1; i <= n; i++) {
    if (par_SCC.count(getParent(i))) {
      deleted.insert(i);
    }
  }
  int ans = deleted.size();
  visited.clear();
  visited.resize(n + 2, false);
  for (int i = 1; i <= n; i++) dsu[i] = i;
  for (int i = 1; i <= n; i++) {
    if (deleted.count(i)) {
      continue;
    }
    for (int next : vec[i]) {
      makeParent(i, next);
    }
  }
  map<int, int> m;
  for (int i = 1; i <= n; i++) {
    if (deleted.count(i)) continue;
    m[getParent(i)]++;
  }
  for (auto it : m) {
    ans += it.second - 1;
  }
  cout << ans << endl;
  return 0;
}
