#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
const int N = 1000010;
vector<long long> g[N], adj[N];
int n, m;
vector<long long> second;
int vis[N];
int cycle[N];
void topo(int x) {
  vis[x] = 1;
  second.push_back(x);
  for (int i = 0; i < g[x].size(); ++i) {
    int y = g[x][i];
    if (vis[y]) continue;
    topo(y);
  }
}
int is_chain;
void dfs(int x) {
  cycle[x] = 1;
  for (int i = 0; i < adj[x].size(); ++i) {
    int y = adj[x][i];
    if (cycle[y] == 1)
      is_chain = 0;
    else if (cycle[y] == 0)
      dfs(y);
  }
  cycle[x] = 2;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    adj[a].push_back(b);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      second.clear();
      topo(i);
      is_chain = 1;
      for (int j = 0; j < second.size(); ++j) {
        if (!cycle[second[j]]) dfs(second[j]);
      };
      ans += (second.size() - is_chain);
    }
  }
  printf("%d\n", ans);
  return 0;
}
