#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(100005);
int cycle;
vector<int> a[MAXN], b[MAXN], component;
bool visited[MAXN];
int color[MAXN];
void dfs(int node) {
  visited[node] = 1;
  component.push_back(node);
  for (int i = 0; i < a[node].size(); i++) {
    int u = a[node][i];
    if (!visited[u]) dfs(u);
  }
}
void dfs2(int node) {
  color[node] = 2;
  for (int i = 0; i < b[node].size(); i++) {
    int u = b[node][i];
    if (color[u] == 0)
      dfs2(u);
    else if (color[u] == 2)
      cycle = 1;
  }
  color[node] = 1;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    a[u].push_back(v);
    a[v].push_back(u);
    b[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
      cycle = 0;
      for (int j = 0; j < component.size(); j++) {
        int u = component[j];
        if (color[u] == 0) dfs2(u);
      }
      ans += component.size() - (!cycle);
      component.clear();
    }
  }
  printf("%d", ans);
}
