#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10, MAX = 2e5 + 1e4, MOD = 1e9 + 7, MAXL = 25;
void OUT(long double o, int x) {
  cout << fixed << setprecision(x) << o;
  return;
}
long long t[MAX], vis[MAX], mo = 0;
vector<int> adj[MAX], vec, ad[MAX], rev[MAX];
void dfs(int v) {
  vis[v] = 1;
  for (int u : adj[v])
    if (!vis[u]) dfs(u);
  vec.push_back(v);
}
void sfd(int v) {
  vis[v] = mo;
  t[mo]++;
  for (int u : rev[v]) {
    if (!vis[u])
      sfd(u);
    else if (vis[u] != mo) {
      ad[mo].push_back(vis[u]);
      ad[vis[u]].push_back(mo);
    }
  }
}
pair<long long, long long> dfs2(int v) {
  vis[v] = 1;
  long long x = 0, y = 0;
  if (t[v] == 1)
    x++;
  else
    y++;
  for (int u : ad[v])
    if (!vis[u]) {
      pair<long long, long long> z = dfs2(u);
      x += z.first, y += z.second;
    }
  return {x, y};
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    rev[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  reverse(vec.begin(), vec.end());
  for (int i = 1; i <= n; i++) vis[i] = 0;
  long long x = 0;
  for (int u : vec)
    if (!vis[u]) {
      mo++;
      sfd(u);
      if (t[mo] != 1) x += t[mo];
    }
  for (int i = 1; i <= n; i++) vis[i] = 0;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      pair<long long, long long> y = dfs2(i);
      x += y.first;
      if (y.second == 0) x--;
    }
  cout << x;
  return 0;
}
