#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int maxn = 100010;
int n, m;
vector<int> adj1[maxn];
vector<int> adj2[maxn];
vector<int> tmp;
int vis1[maxn];
int vis2[maxn];
int iscyc;
void dfs1(int u) {
  vis1[u] = 1;
  tmp.push_back(u);
  for (int i = 0; i < int((adj1[u]).size()); i++) {
    int v = adj1[u][i];
    if (!vis1[v]) dfs1(v);
  }
}
void dfs2(int u) {
  vis2[u] = 1;
  for (int i = 0; i < int((adj2[u]).size()); i++) {
    int v = adj2[u][i];
    if (vis2[v] == 1) iscyc = 1;
    if (!vis2[v]) dfs2(v);
  }
  vis2[u] = 2;
}
void solve() {
  memset(vis1, 0, sizeof(vis1));
  memset(vis2, 0, sizeof(vis2));
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    adj1[a].push_back(b);
    adj1[b].push_back(a);
    adj2[a].push_back(b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (!vis1[i]) {
      tmp.clear();
      dfs1(i);
      iscyc = 0;
      for (int j = 0; j < int((tmp).size()); j++) {
        int u = tmp[j];
        if (!vis2[u]) dfs2(u);
      }
      ans += int((tmp).size()) + iscyc - 1;
    }
  printf("%d", ans);
}
int main() {
  solve();
  return 0;
}
