#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> G[100005];
vector<int> gt[100005];
vector<int> tab;
vector<vector<int> > SCC;
bool looped[100005];
bool was[100005];
bool in[100005];
void dfs(int v, bool yes, int id) {
  was[v] = 1;
  for (int i = 0; i < G[v].size(); i++)
    if (!was[G[v][i]]) dfs(G[v][i], yes, id);
  for (int i = 0; i < gt[v].size(); i++)
    if (!was[gt[v][i]]) dfs(gt[v][i], yes, id);
  if (!yes) {
    tab.push_back(v);
  } else {
    SCC[id].push_back(v);
  }
}
bool dfs2(int v) {
  was[v] = 1;
  in[v] = 1;
  for (int i = 0; i < G[v].size(); i++)
    if (in[G[v][i]])
      return true;
    else if (!was[G[v][i]])
      if (dfs2(G[v][i])) return true;
  in[v] = 0;
  return false;
}
bool use[100005];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    G[a].push_back(b);
    gt[b].push_back(a);
    use[a] = use[b] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (!was[i] && use[i]) dfs(i, false, 0);
  memset(was, 0, sizeof(was));
  int idx = 0;
  for (int i = tab.size() - 1; i >= 0; i--)
    if (!was[tab[i]]) {
      SCC.push_back(vector<int>());
      dfs(tab[i], true, idx);
      idx++;
    }
  memset(was, 0, sizeof(was));
  for (int i = 0; i < SCC.size(); i++) {
    for (int j = 0; j < SCC[i].size(); j++)
      if (!was[SCC[i][j]])
        if (dfs2(SCC[i][j])) {
          looped[i] = true;
          break;
        }
  }
  int result = 0;
  for (int i = 0; i < SCC.size(); i++) {
    if (looped[i])
      result += SCC[i].size();
    else
      result += SCC[i].size() - 1;
  }
  printf("%d\n", result);
}
