#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3228;
int color[N], used[N], lol[N], heh[N], n, m, x, y, pep, ans;
vector<int> v[N], v1[N];
void dfs(int x, int c) {
  color[x] = c;
  heh[c]++;
  for (int i = 0; i < v[x].size(); i++) {
    int to = v[x][i];
    if (color[to] == 0) dfs(to, c);
  }
  for (int i = 0; i < v1[x].size(); i++) {
    int to = v1[x][i];
    if (color[to] == 0) dfs(to, c);
  }
}
void dfs1(int x) {
  used[x] = 2;
  for (int i = 0; i < v[x].size(); i++) {
    int to = v[x][i];
    if (used[to] == 2) {
      lol[color[x]] = 1;
    } else if (used[to] == 0) {
      dfs1(to);
    }
  }
  used[x] = 1;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v1[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (color[i] == 0) {
      pep++;
      dfs(i, pep);
    }
  }
  for (int i = 1; i <= n; i++)
    if (used[i] == 0) dfs1(i);
  for (int i = 1; i <= pep; i++) ans += heh[i] - 1 + lol[i];
  cout << ans;
}
