#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 0.000000001;
const double PI = 3.141592653589793;
const long long LLINF = 99999999999999999LL;
const int MAX_N = 100005;
const int MOD = 1000000007;
int N, M, n, cnt, ans;
vector<int> v[MAX_N], w[MAX_N], SCC[MAX_N], a;
bool cycle;
bool hasCycle[MAX_N], vis[MAX_N];
void DFS1(int x) {
  vis[x] = 1;
  for (int i = 0; i < (int)v[x].size(); ++i) {
    int y = v[x][i];
    if (vis[y] == 0) DFS1(y);
  }
  a.push_back(x);
}
void DFS2(int x) {
  vis[x] = 1;
  SCC[n].push_back(x);
  for (int i = 0; i < (int)w[x].size(); ++i) {
    int y = w[x][i];
    if (vis[y] == 0) DFS2(y);
  }
}
void DFS(int x) {
  ++cnt;
  vis[x] = 1;
  if (hasCycle[x]) cycle = 1;
  for (int i = 0; i < (int)v[x].size(); ++i) {
    int y = v[x][i];
    if (vis[y] == 0) DFS(y);
  }
  for (int i = 0; i < (int)w[x].size(); ++i) {
    int y = w[x][i];
    if (vis[y] == 0) DFS(y);
  }
}
int main() {
  cin >> N >> M;
  for (int i = 1; i <= M; ++i) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    w[y].push_back(x);
  }
  for (int i = 1; i <= N; ++i)
    if (!vis[i]) DFS1(i);
  memset(vis, 0, sizeof(vis));
  for (int i = a.size() - 1; i >= 0; --i)
    if (!vis[a[i]]) {
      ++n;
      DFS2(a[i]);
    }
  for (int i = 1; i <= n; ++i)
    if (SCC[i].size() > 1)
      for (int j = 0; j < (int)SCC[i].size(); ++j) hasCycle[SCC[i][j]] = 1;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= N; ++i) {
    if (vis[i]) continue;
    cnt = 0;
    cycle = 0;
    DFS(i);
    if (cycle)
      ans += cnt;
    else
      ans += cnt - 1;
  }
  cout << ans;
  return 0;
}
