#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> gf[100000 + 3];
vector<int> gb[100000 + 3];
char used[100000 + 3];
char tused[100000 + 3];
vector<int> s;
void tsort(int v) {
  tused[v] = 1;
  for (int i = 0; i < gf[v].size(); i++) {
    int to = gf[v][i];
    if (!tused[to]) {
      tsort(to);
    }
  }
  for (int i = 0; i < gb[v].size(); i++) {
    int to = gb[v][i];
    if (!tused[to]) {
      tsort(to);
    }
  }
  s.push_back(v);
}
bool cycle(int v) {
  used[v] = 2;
  for (int i = 0; i < gf[v].size(); i++) {
    int to = gf[v][i];
    if (used[to] == 2) {
      return true;
    } else if (!used[to] && cycle(to)) {
      return true;
    }
  }
  used[v] = 1;
  return false;
}
int main(int argc, char* argv[]) {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    gf[a].push_back(b);
    gb[b].push_back(a);
  }
  s.reserve(100000 + 3);
  int S = 0;
  for (int i = 0; i < n; i++) {
    if (!tused[i]) {
      s.clear();
      tsort(i);
      bool cycled = false;
      for (int i = 0; i < s.size(); i++) {
        int v = s[i];
        if (!used[v] && cycle(v)) {
          cycled = true;
          break;
        }
      }
      S += (int)s.size() - !cycled;
    }
  }
  printf("%d\n", S);
  return 0;
}
