#include <bits/stdc++.h>
using namespace std;
vector<int> Adj[100001];
vector<int> Edge[100001];
bool visited[100001];
bool checked[100001];
vector<int> v1;
bool circle = false;
bool in_stack[100001];
void DFS_un(int u, int p) {
  visited[u] = true;
  v1.push_back(u);
  for (vector<int>::iterator it = Adj[u].begin(); it != Adj[u].end(); it++) {
    if (!visited[(*it)]) {
      DFS_un((*it), u);
    }
  }
}
void DFS_dir(int u, int p) {
  checked[u] = true;
  in_stack[u] = true;
  for (vector<int>::iterator it = Edge[u].begin(); it != Edge[u].end(); it++) {
    if (in_stack[(*it)]) {
      circle = true;
    }
    if (!checked[(*it)]) {
      DFS_dir((*it), u);
    }
  }
  in_stack[u] = false;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    Adj[a].push_back(b);
    Adj[b].push_back(a);
    Edge[a].push_back(b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      v1.clear();
      DFS_un(i, i);
      if (v1.size() == 1) {
        continue;
      }
      circle = false;
      for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
        if (!checked[(*it)]) {
          DFS_dir((*it), (*it));
        }
      }
      if (circle) {
        ans = ans + v1.size();
      } else {
        ans = ans + v1.size() - 1;
      }
    }
  }
  printf("%d\n", ans);
}
