#include <bits/stdc++.h>
using namespace std;
int f[100001];
vector<int> g[100001];
bool visited[100001];
int t[100001], c = 0;
bool cyc[100001];
int find(int x) {
  if (f[x] != x) f[x] = find(f[x]);
  return f[x];
}
void u(int x, int y) {
  x = find(x);
  y = find(y);
  f[x] = y;
}
void dfs(int x) {
  visited[x] = true;
  c++;
  for (int i = 0; i < g[x].size(); i++)
    if (!visited[g[x][i]]) dfs(g[x][i]);
  c++;
  t[x] = c;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int i, a, b, j;
  for (i = 0; i < n; i++) f[i] = i;
  for (i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    a--;
    b--;
    u(a, b);
    g[a].push_back(b);
  }
  memset(visited, false, sizeof(visited));
  memset(cyc, false, sizeof(false));
  for (i = 0; i < n; i++)
    if (!visited[i]) dfs(i);
  for (i = 0; i < n; i++)
    for (j = 0; j < g[i].size(); j++)
      if (t[i] < t[g[i][j]]) cyc[find(i)] = true;
  set<int> s;
  for (i = 0; i < n; i++)
    if (!cyc[find(i)]) s.insert(find(i));
  printf("%d\n", n - s.size());
  return 0;
}
