#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 123;
int n, m, charger, charge[MAXN], miz;
vector<int> g[MAXN], rev[MAXN];
stack<int> st;
bool mark[MAXN], maze[MAXN], iss;
void inp();
void dfs(int);
void solve();
void rfs(int);
void cfs(int);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  inp();
  solve();
  return 0;
}
void inp() {
  cin >> n >> m;
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    g[--u].push_back(--v), rev[v].push_back(u);
  }
}
void dfs(int v) {
  mark[v] = true;
  for (int u : g[v])
    if (!mark[u]) dfs(u);
  st.push(v);
}
void solve() {
  for (int i = 0; i < n; i++)
    if (!mark[i]) dfs(i);
  memset(mark, 0, sizeof mark);
  while (!st.empty()) {
    if (!mark[st.top()]) {
      miz = 0;
      rfs(st.top());
      maze[charger] = (miz == 1);
      charger++;
    }
    st.pop();
  }
  memset(mark, 0, sizeof mark);
  int w = 0;
  for (int i = 0; i < n; i++)
    if (!mark[i]) {
      iss = true;
      cfs(i);
      if (iss) w++;
    }
  cout << n - w;
}
void rfs(int v) {
  mark[v] = true;
  for (int u : rev[v])
    if (!mark[u]) rfs(u);
  charge[v] = charger;
  miz++;
}
void cfs(int v) {
  mark[v] = true;
  for (int u : g[v])
    if (!mark[u]) cfs(u);
  for (int u : rev[v])
    if (!mark[u]) cfs(u);
  iss &= maze[charge[v]];
}
