#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> v[100005];
int root[100005];
int len[100005];
void init() {
  for (int i = 0; i < n; i++) {
    root[i] = i;
    len[i] = 1;
  }
}
int fin(int x) {
  if (root[x] != x) root[x] = fin(root[x]);
  return root[x];
}
void add(int x, int y) {
  int rx = fin(x), ry = fin(y);
  if (rx == ry) return;
  if (len[rx] > len[ry]) {
    root[ry] = rx;
    len[ry] = len[rx];
  } else {
    root[rx] = ry;
    len[rx] = len[ry];
  }
  if (len[rx] == len[ry]) {
    len[rx]++;
    len[ry]++;
  }
}
int cycle;
int used[100005];
int p[100005];
void dfs(int x) {
  used[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    if (used[v[x][i]] == 1) cycle = 1;
    if (used[v[x][i]] == 0) dfs(v[x][i]);
  }
  used[x] = 2;
}
int main() {
  cin >> n >> m;
  init();
  int ans = n;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      add(i, v[i][j]);
    }
  }
  for (int i = 0; i < n; i++) {
    cycle = 0;
    if (used[i] == 0) dfs(i);
    if (cycle == 1) p[fin(i)] = 1;
  }
  for (int i = 0; i < n; i++) {
    if (fin(i) == i && p[i] == 0) ans--;
  }
  cout << ans;
  return 0;
}
