#include <bits/stdc++.h>
using namespace std;
int n, m, rt[100100], x[100100], y[100100];
int find_rt(int u) { return rt[u] == u ? u : rt[u] = find_rt(rt[u]); }
void link(int u, int v) {
  u = find_rt(u), v = find_rt(v);
  if (u != v) rt[u] = v;
}
vector<int> adj[100100], edge[100100], node[100100];
int C, col[100100], ord[100100], ft;
bool vis[100100];
vector<int> v[100100], rv[100100];
void dfs(int x) {
  vis[x] = 1;
  for (auto y : v[x])
    if (!vis[y]) dfs(y);
  ord[ft--] = x;
}
void rdfs(int x) {
  col[x] = C;
  for (auto y : rv[x])
    if (!col[y]) rdfs(y);
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) rt[i] = i;
  for (int i = 0; i < m; i++) scanf("%d %d", &x[i], &y[i]), link(x[i], y[i]);
  for (int i = 0; i < m; i++) edge[find_rt(x[i])].push_back(i);
  for (int i = 1; i <= n; i++) node[find_rt(i)].push_back(i);
  int rlt = 0;
  for (int i = 1; i <= n; i++) {
    if (node[i].size()) {
      for (auto j : edge[i]) v[x[j]].push_back(y[j]), rv[y[j]].push_back(x[j]);
      int sz = node[i].size();
      ft = sz;
      for (auto u : node[i])
        if (!vis[u]) dfs(u);
      C = 0;
      for (int i = 1; i <= sz; i++)
        if (!col[ord[i]]) C++, rdfs(ord[i]);
      rlt += C == sz ? sz - 1 : sz;
    }
  }
  printf("%d\n", rlt);
  return 0;
}
