#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> Colors, temp;
vector<vector<int>> G, C, GForBfs;
long long ans;
bool dfs(int x);
void bfs(int x);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  G.assign(n, vector<int>(0, 0));
  GForBfs.assign(n, vector<int>(0, 0));
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].push_back(y);
    GForBfs[x].push_back(y);
    GForBfs[y].push_back(x);
  }
  Colors.assign(n, 0);
  for (int i = 0; i < n; i++) {
    if (!Colors[i]) {
      C.push_back(vector<int>(0, 0));
      bfs(i);
    }
  }
  temp.assign(n, 0);
  for (int i = 0; i < C.size(); i++) {
    for (int j = 0; j < C[i].size(); j++) {
      if (dfs(C[i][j])) {
        ans++;
        break;
      }
    }
  }
  for (int i = 0; i < C.size(); i++) {
    ans += max(0, int(C[i].size()) - 1);
  }
  cout << ans;
}
bool dfs(int x) {
  for (int y : G[x]) {
    if (!temp[y]) {
      temp[y] = 1;
      if (dfs(y)) {
        return 1;
      }
    } else if (temp[y] == 1) {
      return 1;
    }
  }
  temp[x] = 2;
  return 0;
}
void bfs(int x) {
  deque<int> O = {x};
  Colors[x] = C.size();
  C[C.size() - 1].push_back(x);
  while (!O.empty()) {
    x = O.front();
    O.pop_front();
    for (int y : GForBfs[x]) {
      if (!Colors[y]) {
        Colors[y] = C.size();
        C[C.size() - 1].push_back(y);
        O.push_back(y);
      }
    }
  }
}
