#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m;
queue<int> q;
int vis[N];
vector<int> no[N];
vector<int> v[N];
int in[N], p[N];
long long ans = 0;
int find(int u) {
  if (u == p[u]) return u;
  return p[u] = find(p[u]);
}
void add(int id) {
  q.empty();
  for (int i = 0; i < no[id].size(); i++)
    if (!in[no[id][i]]) q.push(no[id][i]);
  int x = 0;
  while (q.size()) {
    int u = q.front();
    q.pop();
    x++;
    for (int i = 0; i < v[u].size(); i++) {
      if (!--in[v[u][i]]) q.push(v[u][i]);
    }
  }
  if (x == no[id].size())
    ans += no[id].size() - 1;
  else
    ans += no[id].size();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int a, b, i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
    in[b]++;
    p[find(a)] = find(b);
  }
  for (int i = 1; i <= n; i++) {
    no[find(i)].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] == i) add(i);
  }
  cout << ans << endl;
  return 0;
}
