#include <bits/stdc++.h>
const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int kx[] = {2, 2, -2, -2, 1, -1, 1, -1};
const int ky[] = {1, -1, 1, -1, 2, 2, -2, -2};
bool check(int a, int p) { return (bool)(a & (1 << p)); }
int on(int a, int p) { return (a | (1 << p)); }
using namespace std;
bool fl;
short visit[200010];
bool cycle[200010];
int par[200010];
int size_[200010];
vector<int> adj[200010];
void dfs(int v) {
  visit[v] = 1;
  for (int u : adj[v]) {
    if (visit[u] == 1) {
      fl = 1;
    }
    if (visit[u] == 0) {
      dfs(u);
    }
  }
  visit[v] = 2;
}
int find_(int v) {
  if (v == par[v]) {
    return v;
  }
  return par[v] = find_(par[v]);
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    size_[i] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    int x = find_(a);
    int y = find_(b);
    if (x != y) par[y] = x, size_[x] += size_[y];
  }
  for (int i = 1; i <= n; i++) {
    if (visit[i] == 0) {
      fl = 0;
      dfs(i);
      if (fl) {
        int x = find_(i);
        cycle[x] = 1;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = find_(i);
    if (visit[x] == 2) {
      ans += size_[x] - 1;
      if (cycle[x]) {
        ans++;
      }
      visit[x] = 4;
    }
  }
  cout << ans << endl;
}
