#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100001];
int parent[100001], vis[100001], cnt[100001];
int rep(int a) {
  if (parent[a] == a)
    return a;
  else
    return parent[a] = rep(parent[a]);
}
void dfs(int n) {
  vis[n] = 2;
  for (auto v : graph[n]) {
    if (vis[v] == 0) dfs(v);
    if (vis[v] == 2) cnt[rep(n)] = 0;
  }
  vis[n] = 1;
}
int main() {
  int i, j, k;
  int n, m;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; i++) parent[i] = i;
  for (i = 0; i < m; i++) {
    scanf("%d %d", &j, &k);
    int r1 = rep(j);
    int r2 = rep(k);
    parent[r1] = r2;
    graph[j].push_back(k);
  }
  memset(cnt, -1, sizeof cnt);
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0) dfs(i);
  }
  for (i = 1; i <= n; i++) cnt[rep(i)]++;
  int res = 0;
  for (i = 1; i <= n; i++) res += max(0, cnt[i]);
  printf("%d", res);
}
