#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 18;
const int inf = 1e9;
const int N = 1e5 + 5;
const int M = 2050;
int root[N], n, m, k, h[N], x, y, ans, col[N];
vector<int> v[N];
int findset(int x) { return root[x] = x == root[x] ? x : findset(root[x]); }
void merge(int x, int y) {
  x = findset(x);
  y = findset(y);
  root[y] = x;
}
int dfs(int node) {
  h[node] = 1;
  for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
       it++)
    if (h[*it] == 1)
      return 1;
    else if (h[*it] == 0 && dfs(*it))
      return 1;
  h[node] = 2;
  return 0;
}
int main() {
  scanf("%d %d", &n, &m);
  ans = n;
  for (int i = 1; i <= n; i++) root[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    merge(x, y);
  }
  for (int i = 1; i <= n; i++)
    if (!h[i]) col[findset(i)] = max(col[findset(i)], dfs(i));
  for (int i = 1; i <= n; i++)
    if (root[i] == i) ans -= !col[i];
  return cout << ans << '\n', 0;
}
