#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, m;
int cmpsize[100010];
vector<int> G[100010];
vector<int> rG[100010];
bool vis[100010];
int cmp[100010];
vector<int> vs;
void dfs(int v) {
  vis[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (!vis[G[v][i]]) dfs(G[v][i]);
  }
  vs.push_back(v);
}
void rdfs(int v, int k) {
  vis[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++) {
    if (!vis[rG[v][i]]) rdfs(rG[v][i], k);
  }
}
int scc() {
  memset(vis, 0, sizeof(vis));
  vs.clear();
  for (int i = 0; i < n; i++) {
    if (!vis[i]) dfs(i);
  }
  memset(vis, 0, sizeof(vis));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    if (!vis[vs[i]]) rdfs(vs[i], k++);
  }
  return k;
}
int dfs2(int v) {
  vis[v] = true;
  int ans = cmpsize[cmp[v]] == 1;
  for (int i = 0; i < G[v].size(); i++) {
    if (!vis[G[v][i]]) ans &= dfs2(G[v][i]);
  }
  for (int i = 0; i < rG[v].size(); i++) {
    if (!vis[rG[v][i]]) ans &= dfs2(rG[v][i]);
  }
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    rG[v].push_back(u);
  }
  scc();
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < n; i++) {
    cmpsize[cmp[i]]++;
  }
  int ans = n;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      ans -= dfs2(i);
    }
  }
  cout << ans << endl;
  return 0;
}
