#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 100 + 5;
const long long inf = 9223372036854775807;
int n, m, mark[maxn], comp[maxn], ans, cnt = 1, sz[maxn];
pair<int, int> e[maxn];
vector<int> adj[maxn], g[maxn], radj[maxn], t;
bool dfs2(int v) {
  bool b = true;
  if (sz[v] != 1) b = false;
  mark[v] = 1;
  for (auto u : g[v]) {
    if (mark[u] == 0) {
      b = b & dfs2(u);
    }
  }
  return b;
}
void sfd(int v, int c) {
  comp[v] = c;
  sz[c]++;
  for (auto u : radj[v]) {
    if (comp[u] == 0) {
      sfd(u, c);
    }
  }
}
void dfs(int v) {
  mark[v] = 1;
  for (auto u : adj[v]) {
    if (mark[u] == 0) {
      dfs(u);
    }
  }
  t.push_back(v);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    radj[v].push_back(u);
    e[i] = {u, v};
  }
  for (int i = 1; i <= n; i++) {
    if (mark[i] == 0) {
      dfs(i);
    }
  }
  reverse(t.begin(), t.end());
  for (int i = 0; i < t.size(); i++) {
    if (comp[t[i]] == 0) {
      sfd(t[i], cnt++);
    }
  }
  for (int i = 1; i <= m; i++) {
    int u = e[i].first, v = e[i].second;
    u = comp[u], v = comp[v];
    if (u != v) {
      g[u].push_back(v);
      g[v].push_back(u);
    }
  }
  memset(mark, 0, sizeof mark);
  ans = n;
  for (int i = 1; i < cnt; i++) {
    if (mark[i] == 0) {
      if (dfs2(i)) {
        ans--;
      }
    }
  }
  cout << ans;
}
