#include <bits/stdc++.h>
using namespace std;
bool flag[100005];
int n, m, x, y;
vector<int> a[100005], b[100005];
int d[100005];
int sum, num, ans, dq;
queue<int> q;
int dfs(int x) {
  int sum = 1;
  flag[x] = true;
  if (d[x] == 0) q.push(x);
  for (int i = 0; i < a[x].size(); ++i) {
    int y = a[x][i];
    if (flag[y]) continue;
    sum += dfs(y);
  }
  return sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &x, &y);
    a[x].push_back(y);
    a[y].push_back(x);
    b[x].push_back(y);
    ++d[y];
  }
  memset(flag, false, sizeof(flag));
  for (int i = 1; i <= n; ++i) {
    if (flag[i]) continue;
    sum = dfs(i);
    num = 0;
    while (!q.empty()) {
      int y = q.front();
      q.pop();
      ++num;
      for (int j = 0; j < b[y].size(); ++j) {
        dq = b[y][j];
        --d[dq];
        if (d[dq] == 0) q.push(dq);
      }
    }
    if (num == sum)
      ans += sum - 1;
    else
      ans += sum;
  }
  printf("%d\n", ans);
  return 0;
}
