#include <bits/stdc++.h>
using namespace std;
using LL = int64_t;
const int INF = 0x3fffffff;
const double EPS = 1e-9;
const double PI = 2 * asin(1);
bool g_f;
int g_n, g_m;
vector<bool> vb_mark;
vector<int> vi_mark, vi_num;
vector<vector<int>> vvi_e, vvi_ue;
void pretreat() {}
bool input() {
  cin >> g_n >> g_m;
  if (cin.eof()) return false;
  vb_mark.clear(), vb_mark.resize(g_n + 1), vi_mark.clear();
  vi_mark.resize(g_n + 1), vi_num.clear(), vvi_e.clear();
  vvi_e.resize(g_n + 1), vvi_ue.clear(), vvi_ue.resize(g_n + 1);
  for (int i = 0, u, v; i < g_m; ++i) {
    scanf("%d%d", &u, &v);
    vvi_e[u].emplace_back(v);
    vvi_ue[u].emplace_back(v);
    vvi_ue[v].emplace_back(u);
  }
  return true;
}
void dfs(int u) {
  vi_mark[u] = 1;
  for (auto &v : vvi_e[u]) {
    if (vi_mark[v] == 0)
      dfs(v);
    else if (vi_mark[v] == 1)
      g_f = true;
  }
  vi_mark[u] = 2;
}
void dfs_(int u) {
  vb_mark[u] = true;
  vi_num.emplace_back(u);
  for (auto &v : vvi_ue[u]) {
    if (!vb_mark[v]) dfs_(v);
  }
}
void solve() {
  int ans = 0;
  for (int i = 1; i <= g_n; ++i) {
    if (!vb_mark[i]) {
      vi_num.clear();
      g_f = false;
      dfs_(i);
      for (auto u : vi_num) dfs(u);
      ans += vi_num.size() - 1;
      if (g_f) ++ans;
    }
  }
  cout << ans << endl;
}
int main() {
  pretreat();
  while (input()) {
    solve();
  }
  return 0;
}
