#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const int maxn = 1000 * 100 + 5;
vector<int> aj[maxn];
bool mrk[maxn], live[maxn], ans[maxn];
int cn = 0, u[maxn], v[maxn], cm[maxn];
void dfs(int x, int v) {
  mrk[v] = true;
  live[v] = true;
  for (int i = 0; i < (int)(aj[v]).size(); i++) {
    int u = aj[v][i];
    if (x == 0)
      cm[v] = cn;
    else {
      if (live[u]) ans[cm[u]] = true;
    }
    if (!mrk[u]) dfs(x, u);
  }
  live[v] = false;
  return;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    u[i]--;
    v[i]--;
    aj[u[i]].push_back(v[i]);
    aj[v[i]].push_back(u[i]);
  }
  for (int i = 0; i < n; i++) {
    if (!mrk[i]) {
      cn++;
      dfs(0, i);
    }
  }
  for (int i = 0; i < n; i++) {
    mrk[i] = false;
    aj[i].clear();
  }
  for (int i = 0; i < m; i++) {
    aj[u[i]].push_back(v[i]);
  }
  for (int i = 0; i < n; i++) {
    if (!mrk[i]) {
      dfs(1, i);
    }
  }
  int sum = n;
  for (int i = 1; i <= cn; i++) {
    if (!ans[i]) sum--;
  }
  cout << sum << endl;
  return false;
}
