#include <bits/stdc++.h>
using namespace std;
int n, m;
int a, b;
vector<int> undiradj[100001];
vector<int> diradj[100001];
vector<int> dirjda[100001];
stack<int> s;
bool vis[100001];
vector<int> scc[100001];
bool isinscc[100001];
bool hascycle;
void dfs(int node) {
  if (!vis[node]) {
    vis[node] = true;
    for (int i = 0; i < diradj[node].size(); i++) {
      dfs(diradj[node][i]);
    }
    s.push(node);
  }
}
void sfd(int node, int con) {
  if (!vis[node]) {
    vis[node] = true;
    scc[con].push_back(node);
    for (int i = 0; i < dirjda[node].size(); i++) {
      sfd(dirjda[node][i], con);
    }
  }
}
int undirdfs(int node) {
  if (!vis[node]) {
    if (isinscc[node]) hascycle = true;
    int cnt = 0;
    vis[node] = true;
    for (int i = 0; i < undiradj[node].size(); i++) {
      cnt += undirdfs(undiradj[node][i]);
    }
    cnt++;
    return cnt;
  }
  return 0;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    undiradj[a].push_back(b);
    undiradj[b].push_back(a);
    diradj[a].push_back(b);
    dirjda[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    vis[i] = false;
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    vis[i] = false;
  }
  int cc = 0;
  while (!s.empty()) {
    int curnode = s.top();
    s.pop();
    if (!vis[curnode]) {
      sfd(curnode, cc);
      if (scc[cc].size() > 1) {
        for (int i = 0; i < scc[cc].size(); i++) {
          isinscc[scc[cc][i]] = true;
        }
      }
      cc++;
    }
  }
  for (int i = 1; i <= n; i++) {
    vis[i] = false;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      hascycle = false;
      int v = undirdfs(i);
      if (hascycle)
        ans += v;
      else
        ans += (v - 1);
    }
  }
  cout << ans << '\n';
}
