#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e10;
int a[100100], t1[100100], t2[100100], t, f[100100], p[100100], c[100100];
vector<int> v[100100];
int find(int x) {
  if (x == p[x]) return x;
  return (p[x] = find(p[x]));
}
void un(int x1, int x2) {
  x1 = find(x1);
  x2 = find(x2);
  if (x1 != x2) {
    p[x1] = x2;
    c[x2] += c[x1];
  }
}
void dfs(int x, int p) {
  int i;
  t1[x] = t;
  t2[x] = 0;
  t++;
  a[x] = p;
  for (i = 0; i < v[x].size(); i++)
    if (a[v[x][i]] == -1)
      dfs(v[x][i], x);
    else if (t2[v[x][i]] == 0)
      f[x] = 1;
  t2[x] = t;
  t++;
}
void solve(int n, int m) {
  int j, i, l = 0, w, k, ans = m;
  for (i = 0; i < n; i++) {
    a[i] = -1;
    v[i].clear();
    f[i] = 0;
    t2[i] = 0;
    p[i] = i;
    c[i] = 1;
  }
  for (i = 0; i < m; i++) {
    scanf("%d%d", &l, &k);
    v[l - 1].push_back(k - 1);
    un(l - 1, k - 1);
  }
  t = 0;
  for (i = 0; i < n; i++)
    if (a[i] == -1) dfs(i, 1e5);
  ans = 0;
  for (i = 0; i < n; i++)
    if (f[i] == 1) f[find(i)] = 1;
  for (i = 0; i < n; i++)
    if (find(i) == i && c[i] > 1) {
      if (f[i] == 0)
        ans += c[i] - 1;
      else
        ans += c[i];
    }
  printf("%d\n", ans);
}
int main() {
#pragma comment(linker, "/STACK:1073741824")
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) solve(n, m);
  return 0;
}
