#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int g_n, g_m;
vector<int> vvi_edge[maxn], has[maxn], vvi_uv[maxn];
int vi_dg[maxn];
int mark;
int vi_mark[maxn];
void dfs(int u) {
  vi_mark[u] = mark;
  int len = vvi_edge[u].size();
  for (int i = 0; i < len; i++) {
    int v = vvi_edge[u][i];
    if (vi_mark[v]) continue;
    dfs(v);
  }
}
bool can(int id) {
  int len = has[id].size();
  queue<int> q;
  for (int i = 0; i < len; i++)
    if (vi_dg[has[id][i]] == 0) q.push(has[id][i]);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    len = vvi_uv[u].size();
    for (int i = 0; i < len; i++) {
      vi_dg[vvi_uv[u][i]]--;
      if (!vi_dg[vvi_uv[u][i]]) q.push(vvi_uv[u][i]);
    }
  }
  len = has[id].size();
  for (int i = 0; i < len; i++)
    if (vi_dg[has[id][i]]) return true;
  return false;
}
int main() {
  while (scanf("%d%d", &g_n, &g_m) != EOF) {
    int u, v;
    memset(vi_dg, 0, sizeof(vi_dg));
    for (int i = 0; i <= g_n; i++) {
      vvi_uv[i].clear();
      vvi_edge[i].clear();
      has[i].clear();
    }
    for (int i = 1; i <= g_m; i++) {
      scanf("%d%d", &u, &v);
      vvi_edge[u].push_back(v);
      vvi_edge[v].push_back(u);
      vvi_uv[v].push_back(u);
      vi_dg[u]++;
    }
    mark = 1;
    memset(vi_mark, 0, sizeof(vi_mark));
    for (int i = 1; i <= g_n; i++) {
      if (!vi_mark[i]) {
        dfs(i);
        mark++;
      }
    }
    for (int i = 1; i <= g_n; i++) has[vi_mark[i]].push_back(i);
    int ans = 0;
    for (int i = 1; i < mark; i++)
      if (can(i)) ans++;
    printf("%d\n", g_n - mark + 1 + ans);
  }
  return 0;
}
