#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector<int> graph[maxn];
vector<int> g[maxn];
bool mark[maxn];
int n, m;
int par[maxn];
int cm[maxn];
bool comp[maxn];
int mk[maxn];
int moalefe[maxn];
void dfs(int x, int y) {
  mark[x] = 1;
  cm[x] = y;
  moalefe[y]++;
  for (int v : graph[x])
    if (!mark[v]) dfs(v, y);
}
void ddfs(int x) {
  mk[x] = 1;
  for (int v : g[x]) {
    if (mk[v] == 0)
      ddfs(v);
    else if (mk[v] == 1)
      comp[cm[v]] = 1;
  }
  mk[x] = 2;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    t1--;
    t2--;
    graph[t1].push_back(t2);
    graph[t2].push_back(t1);
    g[t1].push_back(t2);
  }
  int mo = 1;
  for (int i = 0; i < n; i++) {
    if (!mark[i]) {
      dfs(i, mo);
      mo++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (mk[i] == 0) ddfs(i);
  }
  int ans = 0;
  for (int i = 1; i < mo; i++) {
    if (comp[i])
      ans += moalefe[i];
    else
      ans += moalefe[i] - 1;
  }
  cout << ans;
  return 0;
}
