#include <bits/stdc++.h>
using namespace std;
int n, m, ans = 0;
vector<int> G[100010];
vector<int> X[100010];
bool USED[100010];
bool B[100010];
bool S[100010];
void DFS(int v) {
  if (S[v] == true) {
    throw 0;
  }
  if (USED[v] == true) {
    return;
  }
  S[v] = USED[v] = true;
  int i;
  for (i = 0; i < G[v].size(); i++) {
    DFS(G[v][i]);
  }
  S[v] = false;
}
int Q[100010], qb, qe;
void BFS(int v) {
  if (B[v] == true) {
    return;
  }
  qb = qe = 0;
  int i, w, cnt;
  Q[qe++] = v;
  B[v] = true;
  while (qb < qe) {
    v = Q[qb++];
    for (i = 0; i < X[v].size(); i++) {
      w = X[v][i];
      if (B[w] == false) {
        B[w] = true;
        Q[qe++] = w;
      }
    }
  }
  cnt = qe;
  try {
    for (i = 0; i < qe; i++) {
      DFS(Q[i]);
    }
  } catch (int x) {
    cnt++;
  }
  cnt--;
  ans += cnt;
}
int main() {
  int i, a, b;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &a, &b);
    G[a].push_back(b);
    X[a].push_back(b);
    X[b].push_back(a);
  }
  for (i = 1; i <= n; i++) {
    BFS(i);
  }
  printf("%d\n", ans);
  return 0;
}
