#include <bits/stdc++.h>
using namespace std;
const int SZ = 100101;
vector<int> g[SZ], g1[SZ];
vector<int> used;
int c[SZ], c_color[SZ], cnt[SZ];
void dfs(int u) {
  used[u] = -1;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (!used[v])
      dfs(v);
    else if (used[v] == -1)
      c[u] = 1;
  }
  used[u] = 1;
}
void dfs1(int u, int k) {
  used[u] = k;
  cnt[k]++;
  if (c[u]) c_color[k] = 1;
  for (int i = 0; i < g1[u].size(); i++) {
    int v = g1[u][i];
    if (!used[v]) dfs1(v, k);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u - 1].push_back(v - 1);
    g1[u - 1].push_back(v - 1);
    g1[v - 1].push_back(u - 1);
  }
  used.assign(n + 1, 0);
  for (int i = 0; i < n; i++) {
    if (!used[i]) dfs(i);
  }
  int color = 1;
  used.assign(n + 1, 0);
  for (int i = 0; i < n; i++) {
    if (!used[i]) dfs1(i, color++);
  }
  int res = 0;
  for (int i = 1; i < color; i++) res += cnt[i] - 1 + c_color[i];
  cout << res;
}
