#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105000;
int n, m;
int tin[MAXN], tout[MAXN];
vector<int> g[MAXN];
vector<int> gr[MAXN];
vector<int> gg[MAXN];
vector<int> vv;
bool used[MAXN];
int sz[MAXN];
int cursize;
int p[MAXN];
bool has[MAXN];
int ans;
void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < (int)g[v].size(); i++) {
    int to = g[v][i];
    if (!used[to]) dfs(to);
  }
  vv.push_back(v);
}
void dfs2(int v, int par) {
  used[v] = true;
  p[v] = par;
  sz[par]++;
  for (int i = 0; i < (int)gr[v].size(); i++) {
    int to = gr[v][i];
    if (!used[to]) dfs2(to, par);
  }
}
void dfs3(int v) {
  used[v] = true;
  has[v] = true;
  for (int i = 0; i < (int)gg[v].size(); i++) {
    int to = gg[v][i];
    if (!used[to]) dfs3(to);
  }
}
void dfs4(int v) {
  used[v] = true;
  cursize++;
  for (int i = 0; i < (int)gg[v].size(); i++) {
    int to = gg[v][i];
    if (!used[to]) dfs4(to);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int from, to;
    scanf("%d %d", &from, &to);
    g[from].push_back(to);
    gr[to].push_back(from);
    gg[from].push_back(to);
    gg[to].push_back(from);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      dfs(i);
    }
  }
  memset(used, 0, sizeof(used));
  for (int i = 1; i <= n; i++) {
    int cur = vv[n - i];
    if (!used[cur]) {
      dfs2(cur, cur);
      if (sz[cur] > 1) {
        has[cur] = true;
      }
    }
  }
  memset(used, 0, sizeof(used));
  for (int i = 1; i <= n; i++)
    if (!used[i] && has[i]) {
      dfs3(i);
    }
  memset(used, 0, sizeof(used));
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      cursize = 0;
      dfs4(i);
      if (has[i])
        ans += cursize;
      else
        ans += cursize - 1;
    }
  }
  cout << ans << endl;
  return 0;
}
