#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100010;
long long n, m, x, y, res, cycle;
long long cost[maxn];
vector<long long> adj[maxn], alt[maxn];
vector<long long> visited, col;
vector<long long> component;
stack<long long> order;
void dfs1(long long x) {
  col[x] = 1;
  for (auto i : adj[x])
    if (col[i] == 1)
      cycle = 1;
    else if (col[i] == 0)
      dfs1(i);
  col[x] = 2;
}
void dfs2(long long x) {
  visited[x] = true;
  component.push_back(x);
  for (auto i : alt[x])
    if (!visited[i]) dfs2(i);
}
void solve() {
  cin >> n >> m;
  visited.resize(n);
  for (long long i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    alt[y].push_back(x);
    alt[x].push_back(y);
  }
  col.resize(n);
  col.assign(n, 0);
  visited.assign(n, 0);
  for (long long i = 0; i < n; i++) {
    if (!visited[i]) {
      component.clear();
      dfs2(i);
      cycle = 0;
      for (auto x : component)
        if (!col[x]) dfs1(x);
      res += (long long)component.size();
      if (!cycle) res--;
    }
  }
  cout << res;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
