#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> graph[100010];
vector<int> sg[100010];
bool visited[100010];
vector<int> cc[100010];
int cmp;
void dfs(int node) {
  cc[cmp].push_back(node);
  visited[node] = true;
  for (int i = 0; i < (int)sg[node].size(); i++) {
    int next = sg[node][i];
    if (visited[next]) continue;
    dfs(next);
  }
}
bool hascycle[100010];
int dep[100010];
bool cycle(int cur) {
  queue<int> q;
  int sz = (int)cc[cur].size();
  for (int i = 0; i < sz; i++) {
    int node = cc[cur][i];
    if (dep[node] == 0) q.push(node);
  }
  int total = 0;
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    total++;
    for (int i = 0; i < (int)graph[node].size(); i++) {
      int next = graph[node][i];
      dep[next]--;
      if (dep[next] == 0) q.push(next);
    }
  }
  return total != sz;
}
int main() {
  scanf("%d %d", &n, &m);
  memset(dep, 0, sizeof(dep));
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    graph[x].push_back(y);
    dep[y]++;
    sg[x].push_back(y);
    sg[y].push_back(x);
  }
  cmp = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
      cmp++;
    }
  }
  memset(visited, false, sizeof(visited));
  int answer = 0;
  for (int i = 0; i < cmp; i++) {
    bool x = cycle(i);
    int sz = (int)cc[i].size();
    if (x)
      answer += sz;
    else
      answer += (sz - 1);
  }
  printf("%d\n", answer);
  return 0;
}
