#include <bits/stdc++.h>
using namespace std;
vector<int> v[100001], rv[100001], order;
int n, m;
bool vis[100001];
int comp[100001], elecomp[100001];
void rdfs(int node) {
  vis[node] = 1;
  for (int i = 0; i < rv[node].size(); i++) {
    if (!vis[rv[node][i]]) rdfs(rv[node][i]);
  }
  order.push_back(node);
}
void dfs(int node, int k) {
  vis[node] = 1;
  comp[node] = k;
  for (int i = 0; i < v[node].size(); i++) {
    if (!vis[v[node][i]]) dfs(v[node][i], k);
  }
}
int dfs2(int node) {
  vis[node] = 1;
  int ret = (elecomp[comp[node]] == 1);
  for (int i = 0; i < v[node].size(); i++) {
    if (!vis[v[node][i]]) ret = ret & dfs2(v[node][i]);
  }
  for (int i = 0; i < rv[node].size(); i++) {
    if (!vis[rv[node][i]]) ret = ret & dfs2(rv[node][i]);
  }
  return ret;
}
void scc() {
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      rdfs(i);
    }
  }
  memset(vis, 0, sizeof(vis));
  int k = 0;
  for (int i = order.size() - 1; i >= 0; i--) {
    if (!vis[order[i]]) dfs(order[i], k++);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    rv[b].push_back(a);
  }
  scc();
  for (int i = 1; i <= n; i++) {
    elecomp[comp[i]]++;
  }
  int ans = n;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans -= dfs2(i);
    }
  }
  cout << ans;
  return 0;
}
