#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
vector<int> adj[MAX], neg[MAX];
int mark[MAX], mark2[MAX];
bool cycle;
void dfs(int v) {
  mark[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    int u = adj[v][i];
    if (mark[u] == 1) cycle = true;
    if (!mark[u]) dfs(u);
  }
  mark[v] = 2;
}
vector<int> ver;
void go(int v) {
  ver.push_back(v);
  mark2[v] = 1;
  for (int i = 0; i < neg[v].size(); i++) {
    int u = neg[v][i];
    if (!mark2[u]) go(u);
  }
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    neg[u].push_back(v);
    neg[v].push_back(u);
  }
  int ans = n;
  for (int i = 0; i < n; i++)
    if (!mark2[i]) {
      ver.clear();
      go(i);
      cycle = false;
      for (int j = 0; j < ver.size(); j++)
        if (!mark[ver[j]]) dfs(ver[j]);
      if (!cycle) ans--;
    }
  cout << ans << endl;
  return 0;
}
