#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > e, ne;
int n, m;
int dfn[500000], low[500000], TIME, START;
int c, belong[500000], ins[500000];
stack<int> dfsn;
void Tarjan(int v) {
  dfsn.push(v);
  ins[v] = 1;
  dfn[v] = low[v] = ++TIME;
  for (int i = 0; i < e[v].size(); i++) {
    if (dfn[e[v][i]] == 0) {
      Tarjan(e[v][i]);
      low[v] = ((low[v] < low[e[v][i]]) ? (low[v]) : (low[e[v][i]]));
    } else if (ins[e[v][i]]) {
      low[v] = ((low[v] < dfn[e[v][i]]) ? (low[v]) : (dfn[e[v][i]]));
    }
  }
  if (low[v] == dfn[v]) {
    int n;
    c++;
    belong[v] = c;
    while (v != (n = dfsn.top())) {
      belong[n] = c;
      ins[n] = 0;
      dfsn.pop();
    }
    ins[v] = 0;
    dfsn.pop();
  }
}
vector<int> f, block_size, visited;
int getFather(int v) {
  if (f[v] == v) {
    return v;
  }
  return f[v] = getFather(f[v]);
}
int getSize(int v, bool &circle) {
  int res = block_size[v];
  visited[v] = true;
  if (block_size[v] > 1) {
    circle = true;
  }
  for (int i = 0; i < ne[v].size(); i++) {
    if (!visited[ne[v][i]]) res += getSize(ne[v][i], circle);
  }
  return res;
}
int toplogicalSort() {
  vector<int> degree;
  set<int> added;
  degree.resize(c + 1);
  f.resize(c + 1);
  for (int i = 1; i <= c; i++) {
    f[i] = i;
    for (int j = 0; j < ne[i].size(); j++) {
      ne[ne[i][j]].push_back(i);
      degree[ne[i][j]]++;
    }
  }
  visited.resize(c + 1);
  int ans = 0;
  int cur, res = 0, count_head = 0;
  for (int j = 1; j <= c; j++) {
    if (!visited[j]) {
      bool has_circle = false;
      int t = getSize(j, has_circle);
      if (t > 1) {
        if (has_circle) {
          ans += t;
        } else {
          ans += (t - 1);
        }
      }
    }
  }
  return ans;
}
int main() {
  cin >> n >> m;
  e.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!dfn[i]) {
      START = TIME;
      Tarjan(i);
    }
  }
  int ans = 0;
  vector<int> v;
  block_size.resize(c + 1);
  for (int i = 1; i <= n; i++) {
    block_size[belong[i]]++;
  }
  ne.resize(c + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < e[i].size(); j++) {
      if (belong[i] != belong[e[i][j]]) {
        ne[belong[i]].push_back(belong[e[i][j]]);
      }
    }
  }
  ans = toplogicalSort();
  cout << ans << endl;
  return 0;
}
