#include <bits/stdc++.h>
const int maxn = 2e5;
using namespace std;
vector<int> g[maxn], dir[maxn];
int mark[maxn];
int vis[maxn];
bool cycle;
vector<int> an;
void tdfs(int u) {
  vis[u] = 1;
  an.push_back(u);
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (!vis[v]) tdfs(v);
  }
}
void dfs(int u) {
  mark[u] = 1;
  for (int i = 0; i < dir[u].size(); i++) {
    int v = dir[u][i];
    if (!mark[v])
      dfs(v);
    else if (mark[v] == 1)
      cycle = true;
  }
  mark[u] = 2;
}
int main() {
  int n, m, ans;
  scanf("%d%d", &n, &m);
  ans = n;
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
    dir[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      an.clear();
      tdfs(i);
      for (int i = 0; i < an.size(); i++) dfs(an[i]);
      if (!cycle) ans--;
      cycle = false;
    }
  }
  printf("%d\n", ans);
}
