#include <bits/stdc++.h>
using namespace std;
bool used[200005];
vector<int> r;
int n, m, x, y;
int u[200005];
bool cycle;
vector<int> G[200005];
vector<int> g[200005];
void dfs(int v) {
  r.push_back(v);
  used[v] = true;
  for (int i = 0; i < (int)G[v].size(); ++i) {
    int to = G[v][i];
    if (!used[to]) dfs(to);
  }
}
void dfs2(int v) {
  u[v] = 1;
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (u[to] == 0)
      dfs2(to);
    else {
      if (u[to] == 1) {
        cycle = true;
        break;
      }
    }
  }
  u[v] = 2;
}
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &x, &y);
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
    g[x].push_back(y);
  }
  memset(used, false, sizeof(used));
  int ans = n;
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      dfs(i);
      cycle = false;
      for (int j = 0; j < r.size(); ++j) u[r[j]] = 0;
      for (int j = 0; j < r.size(); ++j) {
        int to = r[j];
        if (u[to] == 0) dfs2(to);
      }
      ans -= !cycle;
      r.clear();
    }
  }
  cout << ans << endl;
}
int main() {
  solve();
  return 0;
}
