#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, u, v;
vector<int> g[N];
namespace SCC {
int dfn[N], low[N], id[N], st[N], _st, _, cc;
void dfs(int c, vector<int> g[]) {
  dfn[c] = low[c] = ++cc;
  st[_st++] = c;
  for (auto t : g[c])
    if (!dfn[t])
      dfs(t, g), low[c] = min(low[c], low[t]);
    else if (!id[t])
      low[c] = min(low[c], dfn[t]);
  if (low[c] == dfn[c]) {
    ++_;
    while (st[--_st] != c) id[st[_st]] = _;
    id[c] = _;
  }
}
vector<int> ng[N];
int sz[N], vis[N];
int bfs(int c) {
  vector<int> v;
  v.push_back(c);
  vis[c] = true;
  bool b2 = false;
  int all = 0;
  for (int i = 0; i < ((int)(v).size()); ++i) {
    all += sz[v[i]];
    b2 |= sz[v[i]] >= 2;
    for (auto j : ng[v[i]])
      if (!vis[j]) vis[j] = true, v.push_back(j);
  }
  return all - !b2;
}
int solve(int n, vector<int> g[]) {
  fill_n(dfn, n, cc = 0);
  fill_n(low, n, _st = 0);
  fill_n(id, n, _ = 0);
  for (int i = 0; i < (n); ++i)
    if (!dfn[i]) dfs(i, g);
  fill_n(ng, _, vector<int>());
  fill_n(sz, _, 0);
  fill_n(vis, _, 0);
  int ans = 0;
  for (int i = 0; i < (n); ++i) ++sz[--id[i]];
  for (int i = 0; i < (n); ++i)
    for (auto j : g[i])
      if (id[i] != id[j])
        ng[id[i]].push_back(id[j]), ng[id[j]].push_back(id[i]);
  for (int i = 0; i < (_); ++i)
    if (!vis[i]) ans += bfs(i);
  printf("%d\n", ans);
  return _;
}
}  // namespace SCC
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (m); ++i) {
    scanf("%d%d", &u, &v);
    --u;
    --v;
    g[u].push_back(v);
  }
  SCC::solve(n, g);
  return 0;
}
