#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {
  int v, nxt, sign;
} E[MX];
int Head[MX], rear;
void edge_init() {
  rear = 0;
  memset(Head, -1, sizeof(Head));
}
void edge_add(int u, int v, int s) {
  E[rear].v = v;
  E[rear].nxt = Head[u];
  E[rear].sign = s;
  Head[u] = rear++;
}
int n, m;
int A[MX], cnt[MX], cir[MX], vis[MX], siz;
int DFS1(int u, int id) {
  A[u] = -1;
  int ret = 1;
  for (int i = Head[u]; ~i; i = E[i].nxt) {
    int v = E[i].v;
    if (!A[v]) ret += DFS1(v, id);
  }
  A[u] = id;
  return ret;
}
bool DFS2(int u) {
  vis[u] = -1;
  bool ret = false;
  for (int i = Head[u]; ~i; i = E[i].nxt) {
    int v = E[i].v;
    if (E[i].sign) {
      if (vis[v] == -1) {
        ret = true;
        break;
      }
      if (!vis[v] && DFS2(v)) {
        ret = true;
        break;
      }
    }
  }
  vis[u] = 1;
  return ret;
}
int solve() {
  siz = 0;
  memset(A, 0, sizeof(A));
  memset(cir, 0, sizeof(cir));
  memset(cnt, 0, sizeof(cnt));
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!A[i]) siz++, cnt[siz] = DFS1(i, siz);
    if (!vis[i] && DFS2(i)) cir[A[i]] = 1;
  }
  int ans = 0;
  for (int i = 1; i <= siz; i++) {
    if (cir[i])
      ans += cnt[i];
    else
      ans += cnt[i] - 1;
  }
  return ans;
}
int main() {
  edge_init();
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    edge_add(u, v, 1);
    edge_add(v, u, 0);
  }
  printf("%d\n", solve());
  return 0;
}
