#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
bool mark[N];
int visited[N];
vector<int> ds[N], rev[N], vertices;
void dfs(int u) {
  mark[u] = true;
  vertices.push_back(u);
  for (auto v : ds[u]) {
    if (mark[v]) continue;
    dfs(v);
  }
  for (auto v : rev[u]) {
    if (mark[v]) continue;
    dfs(v);
  }
}
bool visit(int u) {
  visited[u] = 1;
  for (auto v : ds[u]) {
    if (visited[v] == 0) {
      if (visit(v)) return true;
    } else if (visited[v] == 1) {
      return true;
    }
  }
  visited[u] = 2;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    ds[u].push_back(v);
    rev[v].push_back(u);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (mark[i]) continue;
    vertices.clear();
    dfs(i);
    bool cycle = false;
    for (auto u : vertices) {
      if (visited[u] > 0) continue;
      if (visit(u)) {
        cycle = true;
        break;
      }
    }
    if (cycle)
      ans += vertices.size();
    else
      ans += vertices.size() - 1;
  }
  cout << ans;
  return 0;
}
