#include <bits/stdc++.h>
using namespace std;
vector<int> Tp[100001], adj[100001], temp;
int n, m, C, Cyc, backT[100001];
bool vis[100001];
void TopS(int x) {
  vis[x] = 1;
  temp.push_back(x);
  for (int i = 0; i < Tp[x].size(); i++) {
    if (!vis[Tp[x][i]]) TopS(Tp[x][i]);
  }
}
void CheckC(int x) {
  backT[x] = 1;
  for (int i = 0; i < adj[x].size(); i++) {
    if (backT[adj[x][i]] == 1) Cyc = 1;
    if (!backT[adj[x][i]]) CheckC(adj[x][i]);
  }
  backT[x] = 2;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    Tp[a].push_back(b);
    Tp[b].push_back(a);
    adj[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    temp.clear();
    Cyc = 0;
    if (!vis[i]) {
      TopS(i);
      for (int j = 0; j < temp.size(); j++) {
        if (!backT[temp[j]]) CheckC(temp[j]);
      }
      C += temp.size() - 1 + Cyc;
    }
  }
  printf("%d", C);
}
