#include <bits/stdc++.h>
using namespace std;
long long father[100050], tag[100050], siz[100050], add[100050];
vector<long long> a[100050];
long long sum;
long long find(long long x) {
  if (father[x] == x)
    return x;
  else
    return father[x] = find(father[x]);
}
void connect(long long x, long long y) {
  long long j = find(x);
  long long k = find(y);
  if (j == k) return;
  siz[j] += siz[k];
  father[k] = j;
}
void dfs(long long k) {
  long long j;
  tag[k] = 1;
  for (long long i = 0; i < a[k].size(); i++) {
    j = a[k][i];
    if (tag[j] == 0) dfs(j);
    if (tag[j] == 1) add[find(j)] = 1;
  }
  tag[k] = 2;
}
int main() {
  long long j, k, n, m, p, ans, sum = 0;
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) siz[i] = 1, father[i] = i;
  for (long long i = 1; i <= m; i++) {
    cin >> j >> k;
    a[j].push_back(k);
    connect(j, k);
  }
  for (long long i = 1; i <= n; i++)
    if (tag[i] == 0) dfs(i);
  for (long long i = 1; i <= n; i++) {
    if (find(i) == i) sum += siz[i] - 1;
    sum += add[i];
  }
  cout << sum << endl;
  return 0;
}
