#include <bits/stdc++.h>
using namespace std;
const int N = 200000;
int n, m, x, y, ans = 0, r[N], deg[N];
bool vis[N];
vector<int> g[N];
vector<int> v[N];
int find(int x) {
  if (x == r[x]) {
    return r[x];
  }
  return r[x] = find(r[x]);
}
void join(int x, int y) {
  x = find(x);
  y = find(y);
  r[y] = x;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    r[i] = i;
    deg[i] = 0;
    vis[i] = false;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    --x;
    --y;
    ++deg[y];
    join(x, y);
    g[x].push_back(y);
  }
  for (int i = 0; i < n; i++) {
    int z = find(i);
    v[z].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    int z = find(i);
    if (vis[z]) {
      continue;
    }
    queue<int> q;
    for (int j = 0; j < (int)v[z].size(); j++) {
      int u = v[z][j];
      if (deg[u] == 0) {
        vis[u] = true;
        q.push(u);
      }
    }
    if (q.empty()) {
      ans += v[z].size();
    } else {
      bool cyc = false;
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int j = 0; j < (int)g[u].size(); j++) {
          int w = g[u][j];
          if (vis[w]) {
            cyc = true;
            break;
          } else if (--deg[w] == 0) {
            vis[w] = true;
            q.push(w);
          }
        }
        if (cyc) {
          break;
        }
      }
      for (int j = 0; j < (int)v[z].size(); j++) {
        if (!vis[v[z][j]]) {
          cyc = true;
        }
      }
      ans += v[z].size();
      if (!cyc) {
        --ans;
      }
    }
    for (int j = 0; j < (int)v[z].size(); j++) {
      vis[v[z][j]] = true;
    }
  }
  printf("%d\n", ans);
  return 0;
}
