#include <bits/stdc++.h>
using namespace std;
long long modulo(long long base, long long exponent, long long modulus);
long long choose(long long n, long long k);
long long inverse(long long a, long long m);
void build() {}
vector<long long> adj[1000010];
vector<long long> radj[1000010];
long long visit[1000010];
long long comps[1000010];
long long cnt;
void dfs(long long x, stack<long long> &s) {
  if (visit[x]) return;
  visit[x] = 1;
  for (int i = 0; i < adj[x].size(); i++) {
    long long y = adj[x][i];
    if (!visit[y]) dfs(y, s);
  }
  s.push(x);
}
void rdfs(long long x, vector<long long> &curr) {
  visit[x] = 1;
  cnt++;
  for (int i = 0; i < radj[x].size(); i++) {
    long long y = radj[x][i];
    if (!visit[y]) {
      rdfs(y, curr);
    }
  }
  curr.push_back(x);
}
long long ddfs(long long x) {
  visit[x] = 1;
  long long ret;
  if (comps[x] == 1)
    ret = 1;
  else
    ret = 0;
  for (int i = 0; i < adj[x].size(); i++) {
    long long y = adj[x][i];
    if (!visit[y]) ret &= ddfs(y);
  }
  for (int i = 0; i < radj[x].size(); i++) {
    long long y = radj[x][i];
    if (!visit[y]) ret &= ddfs(y);
  }
  return ret;
}
int main() {
  build();
  long long n, m;
  scanf("%lld %lld", &n, &m);
  for (int i = 0; i < m; i++) {
    long long x, y;
    scanf("%lld %lld", &x, &y);
    adj[x].push_back(y);
    radj[y].push_back(x);
  }
  stack<long long> s;
  for (int i = 1; i <= n; i++) {
    if (!visit[i]) dfs(i, s);
  }
  memset(visit, 0, sizeof(visit));
  while (!s.empty()) {
    long long x = s.top();
    s.pop();
    vector<long long> curr;
    if (!visit[x]) {
      cnt = 0;
      rdfs(x, curr);
      for (int i = 0; i < curr.size(); i++) comps[curr[i]] = cnt;
    }
  }
  memset(visit, 0, sizeof(visit));
  long long ans = n;
  for (int i = 1; i <= n; i++) {
    if (!visit[i]) {
      ans -= ddfs(i);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
long long modulo(long long base, long long exponent, long long modulus) {
  if (modulus == 1) return 0;
  long long result = 1;
  base = base % modulus;
  while (exponent > 0) {
    if (exponent % 2 == 1) {
      result = (result * base) % modulus;
    }
    exponent = exponent >> 1;
    base = (base * base) % modulus;
  }
  return result;
}
long long choose(long long n, long long k) {
  if (k == 0) return 1;
  return (n * choose(n - 1, k - 1)) / k;
}
void EE(long long a, long long b, long long &co1, long long &co2) {
  if (a % b == 0) {
    co1 = 0;
    co2 = 1;
    return;
  }
  EE(b, a % b, co1, co2);
  long long temp = co1;
  co1 = co2;
  co2 = temp - co2 * (a / b);
}
long long inverse(long long a, long long m) {
  long long x, y;
  EE(a, m, x, y);
  if (x < 0) x += m;
  return x;
}
