#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e+14;
const int MAXN = 120000;
vector<vector<int> > orient;
vector<vector<int> > nonori;
vector<vector<int> > comps;
vector<char> cl;
int compcnt;
int cycle_st;
bool used[MAXN];
void dfscomp(int v) {
  used[v] = true;
  comps[compcnt].push_back(v);
  for (size_t i = 0; i < nonori[v].size(); ++i) {
    int to = nonori[v][i];
    if (!used[to]) dfscomp(to);
  }
}
bool dfscyc(int v) {
  cl[v] = 1;
  for (size_t i = 0; i < orient[v].size(); ++i) {
    int to = orient[v][i];
    if (cl[to] == 0) {
      if (dfscyc(to)) return true;
    } else if (cl[to] == 1) {
      cycle_st = to;
      return true;
    }
  }
  cl[v] = 2;
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  int i, j, n, m, d, st, a, b, ans = 0;
  cin >> n >> m;
  orient.clear();
  nonori.clear();
  orient.resize(n + 1);
  nonori.resize(n + 1);
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    orient[a].push_back(b);
    nonori[a].push_back(b);
    nonori[b].push_back(a);
  }
  comps.clear();
  for (i = 0; i < MAXN; ++i) used[i] = false;
  for (i = 1; i <= n; ++i)
    if (!used[i]) {
      compcnt = comps.size();
      comps.resize(comps.size() + 1);
      dfscomp(i);
    }
  cl.assign(n + 1, 0);
  for (i = 0; i < comps.size(); i++) {
    cycle_st = -1;
    for (int j = 0; j < comps[i].size(); ++j)
      if (dfscyc(comps[i][j])) break;
    if (cycle_st == -1)
      ans += comps[i].size() - 1;
    else
      ans += comps[i].size();
  }
  cout << ans << endl;
  return 0;
}
