#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("O2")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long int N = 2e5 + 10, mod = 1e9 + 7, inf = 1e18, dlt = 12251;
int poww(int n, int k) {
  if (!k) return 1;
  if (k == 1) return n;
  long long r = poww(n, k / 2);
  return (((r * r) % mod) * poww(n, k & 1)) % mod;
}
int vis[N], comp[N], sz[N];
bool dg[N];
vector<int> adj[N], out[N];
void pre(int v, int c) {
  comp[v] = c;
  sz[c]++;
  for (int u : adj[v])
    if (!comp[u]) pre(u, c);
}
bool dfs(int v) {
  vis[v] = 1;
  for (int u : out[v]) {
    if (vis[u] == 1) return 0;
    if (vis[u] == 2) continue;
    if (!dfs(u)) return 0;
  }
  vis[v] = 2;
  return 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
    out[u].push_back(v);
  }
  int c = 0;
  for (int i = 1; i < n + 1; i++) {
    if (!comp[i]) {
      c++;
      dg[c] = 1;
      pre(i, c);
    }
  }
  for (int i = 1; i < n + 1; i++) {
    if (vis[i]) continue;
    if (!dfs(i)) dg[comp[i]] = 0;
  }
  int ans = 0;
  for (int i = 1; i < c + 1; i++) {
    if (dg[i])
      ans += sz[i] - 1;
    else
      ans += sz[i];
  }
  cout << ans;
  return 0;
}
