#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to;
  int next;
} e[210000], E[310000];
int head[200010], bian, bian2, head2[200010];
void add(int x, int y) {
  bian++;
  e[bian].next = head[x];
  head[x] = bian;
  e[bian].to = y;
}
void add2(int x, int y) {
  bian2++;
  E[bian2].next = head2[x];
  head2[x] = bian2;
  E[bian2].to = y;
}
int low[200010], dnf[200010], vs[200010], cnt, z, sh[200010], mh[200010];
stack<int> q;
void targan(int x) {
  cnt++;
  low[x] = dnf[x] = cnt;
  q.push(x);
  vs[x] = 1;
  for (int i = head[x]; i != -1; i = e[i].next) {
    int y = e[i].to;
    if (!dnf[y]) {
      targan(y);
      low[x] = min(low[x], low[y]);
    } else if (vs[y])
      low[x] = min(low[x], dnf[y]);
  }
  if (low[x] == dnf[x]) {
    z++;
    while (1) {
      int p = q.top();
      q.pop();
      sh[p] = z;
      mh[z]++;
      vs[p] = 0;
      if (p == x) break;
    }
  }
}
int pp;
int vas[200100];
void dfs(int x) {
  vas[x] = 1;
  pp++;
  for (int i = head2[x]; i != -1; i = E[i].next) {
    int y = E[i].to;
    if (!vas[y]) {
      dfs(y);
    }
  }
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(head, -1, sizeof(head));
    bian = 0;
    for (int i = 1; i <= m; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      add(a, b);
    }
    cnt = z = 0;
    memset(low, 0, sizeof(low));
    memset(mh, 0, sizeof(mh));
    memset(dnf, 0, sizeof(dnf));
    memset(sh, 0, sizeof(sh));
    memset(vs, 0, sizeof(vs));
    for (int i = 1; i <= n; i++) {
      if (!dnf[i]) targan(i);
    }
    int zz = 0;
    memset(head2, -1, sizeof(head2));
    bian2 = 0;
    for (int j = 1; j <= n; j++) {
      for (int i = head[j]; i != -1; i = e[i].next) {
        int y = e[i].to;
        add2(j, y);
        add2(y, j);
      }
    }
    int tt = 0;
    memset(vas, 0, sizeof(vas));
    int ha[210000] = {0};
    for (int i = 1; i <= z; i++) {
      if (mh[i] >= 2) {
        for (int j = 1; j <= n; j++) {
          if (sh[j] == i && !vas[j]) {
            tt++;
            pp = 0;
            dfs(j);
            ha[tt] = pp;
            break;
          }
        }
      }
    }
    int qian = tt;
    for (int j = 1; j <= n; j++) {
      if (!vas[j]) {
        tt++;
        pp = 0;
        dfs(j);
        ha[tt] = pp;
      }
    }
    for (int i = 1; i <= qian; i++) {
      if (ha[i] >= 2) zz += ha[i];
    }
    for (int i = qian + 1; i <= tt; i++) zz += (ha[i] - 1);
    printf("%d\n", zz);
  }
  return 0;
}
