#include <bits/stdc++.h>
using namespace std;
const int maxN = 1 << 17;
int cc = 0;
vector<int> T;
struct node {
  bool seen[2];
  vector<int> N[2];
  int c = -2;
} G[maxN];
struct component {
  bool seen;
  vector<int> N;
  int sz = 0;
} D[maxN];
void DFS(int v, int d) {
  G[v].seen[d] = true;
  for (int u : G[v].N[d])
    if (!G[u].seen[d]) DFS(u, d);
  if (!d)
    T.push_back(v);
  else {
    G[v].c = cc;
    D[cc].sz++;
  }
}
pair<int, bool> DFS_C(int v) {
  D[v].seen = true;
  pair<int, bool> R = {D[v].sz, D[v].sz > 1};
  for (int u : D[v].N)
    if (!D[u].seen) {
      pair<int, bool> C = DFS_C(u);
      R.first += C.first;
      R.second |= C.second;
    }
  return R;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].N[0].push_back(v);
    G[v].N[1].push_back(u);
  }
  for (int v = 1; v <= n; v++)
    if (!G[v].seen[0]) DFS(v, 0);
  reverse(T.begin(), T.end());
  for (int v : T)
    if (!G[v].seen[1]) {
      DFS(v, 1);
      cc++;
    }
  for (int v = 1; v <= n; v++)
    for (int u : G[v].N[0])
      if (G[v].c != G[u].c) {
        D[G[v].c].N.push_back(G[u].c);
        D[G[u].c].N.push_back(G[v].c);
      }
  int r = 0;
  for (int v = 0; v < cc; v++)
    if (!D[v].seen) {
      pair<int, bool> c = DFS_C(v);
      r += c.first;
      if (!c.second) r--;
    }
  cout << r;
}
