#include <bits/stdc++.h>
using namespace std;
void prologue() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
const int MAX = 100007;
bool visited[MAX];
vector<vector<int>> g, rg;
vector<int> finish;
vector<int> scc;
void ddfs(int node) {
  if (visited[node]) return;
  visited[node] = true;
  for (int i = 0; i < g[node].size(); ++i) {
    ddfs(g[node][i]);
  }
  finish.push_back(node);
}
void rddfs(int node, int scc_num) {
  if (visited[node]) return;
  visited[node] = true;
  scc[node] = scc_num;
  for (int i = 0; i < rg[node].size(); ++i) {
    rddfs(rg[node][i], scc_num);
  }
}
bool dfs(int node) {
  if (visited[node]) return false;
  visited[node] = true;
  bool cycle_found = false;
  for (int i = 0; i < g[node].size(); ++i) {
    cycle_found |= (scc[node] == scc[g[node][i]]);
    cycle_found |= dfs(g[node][i]);
  }
  for (int i = 0; i < rg[node].size(); ++i) {
    cycle_found |= (scc[node] == scc[rg[node][i]]);
    cycle_found |= dfs(rg[node][i]);
  }
  return cycle_found;
}
int main() {
  prologue();
  int n, m;
  cin >> n >> m;
  g.assign(n, decltype(g)::value_type());
  rg.assign(n, decltype(rg)::value_type());
  scc.assign(n, 0);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    g[u - 1].push_back(v - 1);
    rg[v - 1].push_back(u - 1);
  }
  memset(visited, 0, sizeof(visited));
  for (int i = 0; i < n; ++i) {
    if (!visited[i]) ddfs(i);
  }
  memset(visited, 0, sizeof(visited));
  int scc_num = 0;
  for (int i = finish.size() - 1; i >= 0; --i) {
    if (!visited[finish[i]]) rddfs(finish[i], ++scc_num);
  }
  memset(visited, 0, sizeof(visited));
  int no_cycles = 0;
  for (int i = 0; i < n; ++i) {
    if (!visited[i]) {
      no_cycles += !dfs(i);
    }
  }
  cout << n - no_cycles;
  return 0;
}
