#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj, adj2;
int D[200010];
int I[200010];
int V1[200010];
int V2[200010];
int SEEN, VIS;
void dfs(int u, vector<int> &PV) {
  V1[u] = 1;
  PV.push_back(u);
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i];
    D[v]--;
    if (D[v] == 0) {
      dfs(v, PV);
    }
  }
}
void dfs2(int u, vector<int> &PV) {
  V2[u] = 1;
  PV.push_back(u);
  for (int i = 0; i < adj2[u].size(); ++i) {
    int v = adj2[u][i];
    if (V2[v]) continue;
    dfs2(v, PV);
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  adj = vector<vector<int> >(N);
  adj2 = vector<vector<int> >(N);
  int cnt = 0;
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    D[v]++;
    adj[u].push_back(v);
    cnt += I[u] == 0;
    cnt += I[v] == 0;
    I[u] = I[v] = 1;
    adj2[u].push_back(v);
    adj2[v].push_back(u);
  }
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    if (V2[i]) continue;
    vector<int> CC;
    dfs2(i, CC);
    if (CC.size() <= 1) continue;
    vector<int> PV;
    for (int j = 0; j < CC.size(); ++j) {
      if (V1[CC[j]] == 0 && D[CC[j]] == 0) {
        dfs(CC[j], PV);
      }
    }
    ans += (int)CC.size() - ((int)CC.size() == (int)PV.size());
  }
  cout << ans << endl;
}
