#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
vector<int> g[MAXN];
vector<int> gd[MAXN];
bool mk[MAXN];
int cnt;
vector<int> tmp;
void dfs(int u) {
  if (mk[u]) {
    return;
  }
  mk[u] = true;
  tmp.push_back(u);
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    dfs(v);
  }
}
bool mk1[MAXN], mk2[MAXN];
bool cycle(int u, int p) {
  if (mk2[u]) {
    return true;
  }
  if (mk1[u]) {
    return false;
  }
  mk1[u] = mk2[u] = true;
  for (int i = 0; i < gd[u].size(); i++) {
    int v = gd[u][i];
    if (cycle(v, u)) {
      return true;
    }
  }
  mk2[u] = false;
  return false;
}
int solve(int u) {
  cnt = 0;
  tmp.clear();
  dfs(u);
  int sol = tmp.size() - 1;
  for (int i = 0; i < tmp.size(); i++) {
    int v = tmp[i];
    if (cycle(v, -1)) {
      sol++;
      break;
    }
  }
  return sol;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
    gd[u].push_back(v);
  }
  int sol = 0;
  for (int u = 1; u <= n; u++) {
    if (!mk[u]) {
      sol += solve(u);
    }
  }
  cout << sol << '\n';
}
