#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * power(a * a % md, b / 2, md) % md
                      : power(a * a % md, b / 2, md) % md));
}
const int xn = 3e5 + 10;
const int xm = -20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const long long INF = 1e18 + 10;
const int mod = 998244353;
const int base = 257;
int n, m, ptr, col[xn], res, a[xn], ans;
pair<int, int> E[xn];
vector<int> adj[2][xn], G[2][xn], vec[xn], topo;
bool mark[xn], flag[xn];
void DFS(int v) {
  mark[v] = true;
  for (int u : adj[0][v])
    if (!mark[u]) DFS(u);
  topo.push_back(v);
}
void SFD(int v, int c) {
  mark[v] = true;
  col[v] = c;
  vec[c].push_back(v);
  for (int u : adj[1][v])
    if (!mark[u]) SFD(u, c);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int v, u;
    cin >> v >> u;
    adj[0][v].push_back(u);
    adj[1][u].push_back(v);
    E[i] = {v, u};
  }
  for (int i = 1; i <= n; ++i)
    if (!mark[i]) DFS(i);
  reverse(topo.begin(), topo.end());
  memset(mark, false, sizeof mark);
  for (int v : topo)
    if (!mark[v]) SFD(v, ++ptr);
  for (int i = 1; i <= m; ++i) {
    int v = E[i].first, u = E[i].second;
    if (col[v] == col[u]) continue;
    G[0][col[v]].push_back(col[u]);
    G[1][col[u]].push_back(col[v]);
  }
  memset(mark, false, sizeof mark);
  for (int v = 1; v <= ptr; ++v) {
    for (int u : G[1][v]) {
      if (mark[u]) continue;
      mark[u] = true;
      for (int w : G[1][u]) {
        res -= 1 - flag[w];
        flag[w] = true;
        res += 1 - flag[w];
      }
    }
    for (int u : G[1][v]) res -= 1 - flag[u];
    a[v] += res;
    for (int u : G[1][v]) res += 1 - flag[u];
    res += 1;
  }
  memset(mark, false, sizeof mark);
  memset(flag, false, sizeof flag);
  res = 0;
  for (int v = ptr; v >= 1; --v) {
    for (int u : G[0][v]) {
      if (mark[u]) continue;
      mark[u] = true;
      for (int w : G[0][u]) {
        res -= 1 - flag[w];
        flag[w] = true;
        res += 1 - flag[w];
      }
    }
    for (int u : G[0][v]) res -= 1 - flag[u];
    a[v] += res;
    for (int u : G[0][v]) res += 1 - flag[u];
    if (a[v] < 2) ans += int(vec[v].size());
    res += 1;
  }
  cout << ans << '\n';
  return 0;
}
