#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
int n, m;
int Stop, Bcnt, Dindex, DFN[MAX], LOW[MAX], Stap[MAX], Belong[MAX], num[MAX],
    used[MAX];
bool instack[MAX];
vector<int> G[MAX], g[MAX], gg;
void tarjan(int i) {
  int j;
  DFN[i] = LOW[i] = ++Dindex;
  instack[i] = true;
  Stap[++Stop] = i;
  for (int v = 0; v < G[i].size(); ++v) {
    j = G[i][v];
    if (!DFN[j]) {
      tarjan(j);
      if (LOW[j] < LOW[i]) LOW[i] = LOW[j];
    } else if (instack[j] && DFN[j] < LOW[i])
      LOW[i] = DFN[j];
  }
  if (DFN[i] == LOW[i]) {
    Bcnt++;
    do {
      j = Stap[Stop--];
      instack[j] = false;
      Belong[j] = Bcnt;
    } while (j != i);
  }
}
void bfs(int s, int id) {
  queue<int> que;
  que.push(s);
  used[s] = id;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    gg.push_back(u);
    for (int i = 0; i < g[u].size(); ++i) {
      int v = g[u][i];
      if (used[v] != id) {
        que.push(v);
        used[v] = id;
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (!DFN[i]) {
      gg.clear();
      bfs(i, i);
      for (int i = 0; i < gg.size(); ++i) {
        if (!DFN[gg[i]]) tarjan(gg[i]);
      }
      int flag = 1;
      for (int i = 0; i < gg.size(); ++i) {
        num[Belong[gg[i]]]++;
        if (num[Belong[gg[i]]] > 1) flag = 0;
      }
      ans += gg.size() - flag;
    }
  }
  printf("%d\n", ans);
  return 0;
}
