#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
bool used[maxn];
vector<int> vs;
vector<int> g[maxn], rg[maxn];
int cmp[maxn], cmpsize[maxn];
int n, m, x, y;
void dfs(int pos) {
  used[pos] = true;
  for (int i = 0; i != g[pos].size(); i++)
    if (!used[g[pos][i]]) dfs(g[pos][i]);
  vs.push_back(pos);
}
void rdfs(int pos, int k) {
  used[pos] = true;
  cmp[pos] = k;
  for (int i = 0; i != rg[pos].size(); i++)
    if (!used[rg[pos][i]]) rdfs(rg[pos][i], k);
}
void scc() {
  memset(used, false, sizeof(used));
  vs.clear();
  for (int i = 0; i != n; i++)
    if (!used[i]) dfs(i);
  memset(used, false, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i != -1; i--)
    if (!used[vs[i]]) rdfs(vs[i], k++);
}
bool dfs1(int pos) {
  used[pos] = true;
  bool ans = cmpsize[cmp[pos]] == 1;
  for (int i = 0; i != g[pos].size(); i++)
    if (!used[g[pos][i]]) ans &= dfs1(g[pos][i]) == true;
  for (int i = 0; i != rg[pos].size(); i++)
    if (!used[rg[pos][i]]) ans &= dfs1(rg[pos][i]) == true;
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i != m; i++) {
    scanf("%d%d", &x, &y);
    g[x - 1].push_back(y - 1);
    rg[y - 1].push_back(x - 1);
  }
  scc();
  memset(used, false, sizeof(used));
  for (int i = 0; i != n; i++) cmpsize[cmp[i]]++;
  int ans = n;
  for (int i = 0; i != n; i++)
    if (!used[i]) ans -= dfs1(i);
  printf("%d\n", ans);
  return 0;
}
