#include <bits/stdc++.h>
inline void in(int &MAGNUS) { scanf("%d", &MAGNUS); }
inline void out(int MAGNUS) { printf("%d\n", MAGNUS); }
inline void in(int &MAGNUS, int &CLAR) { scanf("%d%d", &MAGNUS, &CLAR); }
inline void out(int MAGNUS, int CLAR) { printf("%d %d\n", MAGNUS, CLAR); }
inline void inl(long long &LIV) { scanf("%lld", &LIV); }
inline void inl(long long &LIV, long long &MART) {
  scanf("%lld%lld", &LIV, &MART);
}
inline void outl(long long LIV) { printf("%lld\n", LIV); }
inline void outl(long long LIV, long long MART) {
  printf("%lld %lld\n", LIV, MART);
}
using namespace std;
int N, E;
int ccid[100005], ccsize[100005];
vector<int> vertex, graf[100005], igraf[100005];
bool vis[100005];
void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i <= (int)graf[u].size() - 1; i++) {
    int v = graf[u][i];
    if (vis[v]) continue;
    dfs(v);
  }
  vertex.push_back(u);
}
void dfs2(int u, int id) {
  vis[u] = 1;
  ccid[u] = id;
  for (int i = 0; i <= (int)igraf[u].size() - 1; i++) {
    int v = igraf[u][i];
    if (vis[v]) continue;
    dfs2(v, id);
  }
}
bool nocycle(int u) {
  vis[u] = 1;
  bool cycle = ccsize[ccid[u]] == 1;
  for (int i = 0; i <= (int)graf[u].size() - 1; i++) {
    int v = graf[u][i];
    if (vis[v]) continue;
    cycle &= nocycle(v);
  }
  for (int i = 0; i <= (int)igraf[u].size() - 1; i++) {
    int v = igraf[u][i];
    if (vis[v]) continue;
    cycle &= nocycle(v);
  }
  return cycle;
}
int main() {
  in(N, E);
  for (int i = 1; i <= E; i++) {
    int u, v;
    in(u, v);
    graf[u].push_back(v);
    igraf[v].push_back(u);
  }
  for (int i = 1; i <= N; i++) {
    if (!vis[i]) dfs(i);
  }
  memset(vis, 0, sizeof(vis));
  int ccmany = 1;
  reverse((vertex).begin(), (vertex).end());
  for (int i = 0; i <= (int)vertex.size() - 1; i++)
    if (!vis[vertex[i]]) dfs2(vertex[i], ccmany++);
  ccmany--;
  memset(ccsize, 0, sizeof(ccsize));
  for (int i = 1; i <= N; i++) ccsize[ccid[i]]++;
  int ans = N;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= N; i++) {
    if (!vis[i]) ans -= nocycle(i);
  }
  out(ans);
  return 0;
}
