#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005], dir[100005], vec;
int vis1[100005], vis2[100005], vis3[100005], fl = 0;
void dfs1(int u) {
  vis1[u] = 1;
  vec.push_back(u);
  for (int i = (int)0; i <= (int)adj[u].size() - 1; i++) {
    if (!vis1[adj[u][i]]) dfs1(adj[u][i]);
  }
}
void dfs2(int u) {
  vis2[u] = 1;
  vis3[u] = 1;
  for (int i = (int)0; i <= (int)dir[u].size() - 1; i++) {
    if (!vis2[dir[u][i]]) dfs2(dir[u][i]);
    if (vis3[dir[u][i]]) fl = 1;
  }
  vis3[u] = 0;
}
int fun() {
  int ret = 0;
  for (int j = (int)0; j <= (int)vec.size() - 1; j++) {
    if (!vis2[vec[j]]) {
      fl = 0;
      dfs2(vec[j]);
      ret += fl;
    }
  }
  return (ret == 0);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = (int)1; i <= (int)m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
    dir[u].push_back(v);
  }
  int ans = 0;
  for (int i = (int)1; i <= (int)n; i++) {
    if (!vis1[i]) {
      vec.clear();
      dfs1(i);
      ans += vec.size() - fun();
    }
  }
  cout << ans << endl;
  return 0;
}
