#include <bits/stdc++.h>
using namespace std;
int const Max = 2e5 + 10;
bool mark[Max];
int col[Max];
int ans = 0, mo = 0, cnt = 0;
bool flag = 0;
int sv[Max];
vector<int> adj[Max], adj2[Max];
vector<int> st;
vector<int> ne[Max];
void dfs(int v) {
  mark[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    int u = adj[v][i];
    if (mark[u]) continue;
    dfs(u);
  }
  st.push_back(v);
}
void bdfs(int v) {
  col[v] = mo;
  cnt++;
  for (int i = 0; i < adj2[v].size(); i++) {
    int u = adj2[v][i];
    if (col[u] != mo && col[u]) {
      ne[col[u]].push_back(col[v]), ne[col[v]].push_back(col[u]);
    }
    if (!col[u]) bdfs(u);
  }
}
void new_dfs(int v) {
  cnt += sv[v];
  mark[v] = 1;
  if (sv[v] > 1) flag = 1;
  for (int i = 0; i < ne[v].size(); i++) {
    int u = ne[v][i];
    if (!mark[u]) new_dfs(u);
  }
}
int main() {
  int n, a, b, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> a >> b, adj[a].push_back(b), adj2[b].push_back(a);
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) dfs(i);
  }
  reverse(st.begin(), st.end());
  for (int i = 0; i < st.size(); i++) {
    if (!col[st[i]]) {
      cnt = 0, mo++;
      bdfs(st[i]);
      sv[mo] = cnt;
    }
  }
  for (int i = 1; i <= n; i++) mark[i] = 0;
  for (int i = 1; i <= mo; i++) {
    cnt = 0;
    if (!mark[i]) flag = 0, new_dfs(i), ans += cnt - 1 + flag;
  }
  cout << ans;
}
