#include <bits/stdc++.h>
using namespace std;
vector<int> vis;
vector<vector<int> > adj, adj2;
long long ans;
int wcc[(long long)3e5 + 100], scc[(long long)3e5 + 100],
    ssz[(long long)3e5 + 100], dfs_low[(long long)3e5 + 100],
    dfs_num[(long long)3e5 + 100], wccsz[(long long)3e5 + 100];
int scnum;
stack<int> s;
void opt() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int cnt = 0;
int cnt2 = 0;
void dfs(int u) {
  vis[u] = 1;
  wcc[u] = cnt;
  cnt2++;
  for (int i = 0; i < adj2[u].size(); i++) {
    int v = adj2[u][i];
    if (!vis[v]) dfs(v);
  }
}
int ds;
void tarjan(int u) {
  dfs_low[u] = dfs_num[u] = ds++;
  s.push(u);
  vis[u] = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (dfs_num[v] == 0) {
      tarjan(v);
    }
    if (vis[v]) dfs_low[u] = min(dfs_low[u], dfs_low[v]);
  }
  if (dfs_low[u] == dfs_num[u]) {
    int sz = 0;
    while (1) {
      int v = s.top();
      s.pop();
      vis[v] = 0;
      scc[v] = scnum;
      sz++;
      if (u == v) break;
    }
    ssz[scnum++] = sz;
  }
}
bool taken[(long long)3e5 + 100];
int main() {
  opt();
  int m, n;
  cin >> n >> m;
  adj.assign(n + 3, vector<int>());
  adj2.assign(n + 3, vector<int>());
  vis.assign(n + 3, 0);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].push_back(y);
    if (!binary_search(adj2[x].begin(), adj2[x].end(), y)) adj2[x].push_back(y);
    if (!binary_search(adj2[y].begin(), adj2[y].end(), x)) adj2[y].push_back(x);
  }
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      dfs(i);
      wccsz[cnt++] = cnt2;
      cnt2 = 0;
    }
  vis.clear();
  vis.assign(n + 3, 0);
  for (int i = 0; i < n; i++) {
    if (dfs_num[i] == 0) tarjan(i);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!taken[wcc[i]]) {
      ans += wccsz[wcc[i]] - 1;
      taken[wcc[i]] = 1;
    }
  }
  for (int i = 0; i <= n; i++) taken[i] = 0;
  for (int i = 0; i < n; i++) {
    if (ssz[scc[i]] > 1 && !taken[wcc[i]]) {
      ans++;
      taken[wcc[i]] = 1;
    }
  }
  cout << ans;
}
