#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 7;
int NUMBER_OF_WAYS, n, m, u, vertex, conectivity_marker[MAX + 1], component,
    teleport_marker[MAX + 1], components[MAX + 1];
vector<int> conectivity[MAX + 1], teleport[MAX + 1];
void DEE_EF_ES(int source) {
  conectivity_marker[source] = component;
  for (auto member : conectivity[source]) {
    if (!conectivity_marker[member]) {
      DEE_EF_ES(member);
    }
  }
}
void dfs(int leaf) {
  teleport_marker[leaf] = 2;
  for (auto memser : teleport[leaf]) {
    if (teleport_marker[memser] == 2) {
      components[conectivity_marker[memser]] = 1;
    } else if (teleport_marker[memser] == 0) {
      dfs(memser);
    }
  }
  teleport_marker[leaf] = 1;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u >> vertex;
    teleport[u].push_back(vertex);
    conectivity[u].push_back(vertex);
    conectivity[vertex].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (conectivity_marker[i] == 0) {
      component++;
      DEE_EF_ES(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!teleport_marker[MAX]) {
      dfs(i);
    }
  }
  NUMBER_OF_WAYS = n - component;
  for (int i = 1; i <= n; i++) {
    NUMBER_OF_WAYS += components[i];
  }
  cout << NUMBER_OF_WAYS << endl;
  return 0;
}
