#include <bits/stdc++.h>
using namespace std;
int const lim = 2e5 + 10;
int ans = 0, scc = 0, mn[lim + 3], val[lim + 3], t = 1;
stack<int> cur;
bool vis[lim + 3];
vector<int> adj[lim + 3], adj1[lim + 3];
int anss = 0;
void dfs(int u) {
  mn[u] = val[u] = t++;
  cur.push(u);
  vis[u] = true;
  for (int i = 0; i < adj[u].size(); i++) {
    int che = adj[u][i];
    if (!val[che])
      dfs(che), mn[u] = min(mn[u], mn[che]);
    else if (vis[che])
      mn[u] = min(mn[u], val[che]);
  }
  if (mn[u] == val[u]) {
    int con = 0;
    while (cur.size() && cur.top() != u)
      con++, vis[cur.top()] = false, cur.pop();
    con++;
    cur.pop();
    vis[u] = false;
    if (con > 1) ans += con;
    scc++;
  }
}
bool vis1[lim + 3];
vector<int> nodes;
void dfs1(int u) {
  vis1[u] = true;
  nodes.push_back(u);
  for (int i = 0; i < adj1[u].size(); i++) {
    int che = adj1[u][i];
    if (!vis1[che]) dfs1(che);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj1[u].push_back(v);
    adj1[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (val[i]) continue;
    scc = 0;
    ans = 0;
    nodes.clear();
    dfs1(i);
    for (int j = 0; j < nodes.size(); j++)
      if (!val[nodes[j]]) dfs(nodes[j]);
    int sz = nodes.size();
    anss += min(ans + scc - 1, sz);
  }
  cout << anss << endl;
}
