#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> g[100001];
vector<int> gg[100001];
bool vis[100001];
int bel[100001];
void dfs1(int x, int w) {
  bel[x] = w;
  vis[x] = 1;
  for (int i = 0; i < gg[x].size(); i++) {
    int v = gg[x][i];
    if (vis[v]) continue;
    dfs1(v, w);
  }
}
int degree[100001];
vector<int> pk[100001];
int ans = 0;
queue<int> q;
void solve(int x) {
  int num = 0;
  for (int i = 0; i < pk[x].size(); i++) {
    int a = pk[x][i];
    if (degree[a] == 0) {
      num++;
      q.push(a);
    }
  }
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    for (int i = 0; i < g[a].size(); i++) {
      int v = g[a][i];
      degree[v]--;
      if (degree[v] == 0) {
        num++;
        q.push(v);
      }
    }
  }
  if (num == pk[x].size())
    ans += num - 1;
  else
    ans += pk[x].size();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b; i <= m; i++) {
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    degree[b]++;
    gg[a].push_back(b);
    gg[b].push_back(a);
  }
  int o = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      o++;
      dfs1(i, o);
    }
  }
  for (int i = 1; i <= n; i++) {
    pk[bel[i]].push_back(i);
  }
  for (int i = 1; i <= o; i++) {
    solve(i);
  }
  printf("%d\n", ans);
  return 0;
}
