#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector<int> nei[maxn], nei2[maxn];
int n, m, cnt;
bool cycle = false;
int col[maxn];
int mark[maxn], ans;
bool cmp[maxn];
void dfs(int v) {
  col[v] = ans;
  mark[v] = 1;
  for (auto u : nei[v]) {
    if (!mark[u]) {
      dfs(u);
    }
  }
}
void dfs2(int v) {
  mark[v] = 1;
  for (auto u : nei2[v]) {
    if (!mark[u])
      dfs2(u);
    else if (mark[u] == 1) {
      cmp[col[u]] = true;
    }
  }
  mark[v] = 2;
}
void dfs_all() {
  for (int i = 0; i < n; i++) {
    if (!mark[i]) {
      dfs(i);
      ans++;
    }
  }
  memset(mark, 0, sizeof mark);
  for (int i = 0; i < n; i++)
    if (!mark[i]) {
      dfs2(i);
    }
  for (int i = 0; i < ans; i++)
    if (cmp[i]) cnt++;
  cout << n - ans + cnt << endl;
}
int main() {
  cin >> n >> m;
  for (int i = 0, a, b; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    nei[a].push_back(b), nei[b].push_back(a);
    nei2[a].push_back(b);
  }
  dfs_all();
}
