#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long mod2 = 1271309838631957LL;
long double PI = acosl(-1);
const long long inf = 1e15;
const int nmax = 3e5 + 10;
const int MAXLG = log2(nmax) + 1;
vector<int> g[nmax];
vector<int> grev[nmax];
int n, m, hui;
int indeg[nmax], outdeg[nmax], mxchain[nmax], veg[nmax], reveg[nmax];
bool vis[nmax];
pair<int, int> pera[nmax];
vector<int> eul;
int mein[nmax], upormein[nmax], nichemain[nmax];
void dfs1(int u) {
  if (vis[u]) return;
  vis[u] = true;
  mxchain[u] = 1;
  for (auto v : g[u]) {
    dfs1(v);
    mxchain[u] = max(mxchain[u], mxchain[v] + 1);
  }
}
int dfs3(int u, bool mara = false) {
  if (vis[u] or mein[u]) return 0;
  vis[u] = true;
  int ret = 1;
  veg[u] = 0;
  if (mara) eul.push_back(u);
  for (auto v : g[u]) {
    ret += dfs3(v, mara);
  }
  return veg[u] = ret;
}
int dfs6(int u, bool mara = false) {
  if (vis[u] or mein[u]) return 0;
  vis[u] = true;
  int ret = 1;
  reveg[u] = 0;
  if (mara) eul.push_back(u);
  for (auto v : grev[u]) {
    ret += dfs6(v, mara);
  }
  return reveg[u] = ret;
}
void dfs4(int u) {
  if (vis[u] or mein[u]) return;
  vis[u] = true;
  upormein[u] = 1;
  for (auto v : grev[u]) {
    dfs4(v);
    upormein[u] = max(upormein[u], upormein[v]);
  }
}
void dfs5(int u) {
  if (vis[u] or mein[u]) return;
  vis[u] = true;
  nichemain[u] = n + 3;
  for (auto v : g[u]) {
    dfs5(v);
    nichemain[u] = min(nichemain[u], nichemain[v]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v), grev[v].push_back(u);
    indeg[v]++, outdeg[u]++;
  }
  for (int i = 1; i <= n; i++) {
    if (outdeg[i] == 0) g[i].push_back(n + 2), grev[n + 2].push_back(i);
    if (indeg[i] == 0) g[n + 1].push_back(i), grev[i].push_back(n + 1);
  }
  dfs1(n + 1);
  int ans = 0, sorce = n + 1, sink = n + 2;
  vector<int> mainchain;
  mainchain.push_back(sorce);
  mein[sorce] = ++hui;
  while (1) {
    int child = 0, beshi = -1;
    for (auto x : g[sorce])
      if (beshi < mxchain[x]) child = x, beshi = mxchain[x];
    sorce = child;
    mainchain.push_back(child), mein[child] = ++hui;
    if (child == sink) break;
  }
  for (int i = 0; i < mainchain.size(); i++)
    upormein[mainchain[i]] = nichemain[mainchain[i]] = mein[mainchain[i]];
  memset(vis, false, sizeof vis);
  for (int i = 1; i <= n; i++)
    if (!mein[i] and !vis[i]) dfs4(i);
  memset(vis, false, sizeof vis);
  for (int i = 1; i <= n; i++)
    if (!mein[i] and !vis[i]) dfs5(i);
  memset(vis, false, sizeof vis);
  for (int i = mainchain.size() - 1; i >= 0; i--) {
    int u = mainchain[i];
    int x = 0;
    for (auto v : g[u]) {
      x += dfs3(v);
    }
    vector<int> tmp;
    for (auto v : grev[u]) {
      if (nichemain[v] - upormein[v] == 2 and nichemain[v] - 1 == i)
        tmp.push_back(v);
    }
    if (tmp.size() == 1) {
      int startnod = tmp.back();
      dfs3(startnod, true);
      pera[startnod].first = veg[startnod];
      for (auto x : eul) vis[x] = false;
      eul.clear();
    }
    if (i < mainchain.size() - 1)
      veg[u] = x + 1 + veg[mainchain[i + 1]];
    else
      veg[u] = x + 1;
  }
  memset(vis, false, sizeof vis);
  for (int i = 0; i < mainchain.size(); i++) {
    int u = mainchain[i];
    int x = 0;
    for (auto v : grev[u]) {
      x += dfs6(v);
    }
    vector<int> tmp;
    for (auto v : g[u]) {
      if (nichemain[v] - upormein[v] == 2 and upormein[v] - 1 == i)
        tmp.push_back(v);
    }
    if (tmp.size() == 1) {
      int startnod = tmp.back();
      dfs6(startnod, true);
      pera[startnod].second = reveg[startnod];
      for (auto x : eul) vis[x] = false;
      eul.clear();
    }
    if (i)
      reveg[u] = x + 1 + reveg[mainchain[i - 1]];
    else
      reveg[u] = x + 1;
  }
  for (int i = 1; i <= n; i++) {
    if (!mein[i]) {
      if (nichemain[i] - upormein[i] == 2) {
        if (pera[i].first + veg[mainchain[nichemain[i] - 1]] + pera[i].second +
                reveg[mainchain[upormein[i] - 1]] ==
            n + 2) {
          ans++;
        }
      }
    }
  }
  for (int i = 1; i < mainchain.size() - 1; i++) {
    int u = mainchain[i];
    if (veg[u] + reveg[u] >= n + 2) {
      ans++;
    }
  }
  cout << ans;
}
