#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5;
vector<int> adj[MAX + 1], rev_adj[MAX + 1], undirected[MAX + 1];
vector<bool> visited(MAX + 1, -1);
vector<vector<int> > dif_topo, scc, sub_graph;
void dfs(int u) {
  visited[u] = 1;
  sub_graph.back().push_back(u);
  for (auto v : undirected[u]) {
    if (visited[v] == 0) dfs(v);
  }
}
void dfs1(int u) {
  visited[u] = 1;
  for (auto v : adj[u]) {
    if (visited[v] == 0) dfs1(v);
  }
  dif_topo.back().push_back(u);
}
void dfs2(int u) {
  visited[u] = 1;
  scc.back().back()++;
  for (auto v : rev_adj[u]) {
    if (visited[v] == 0) dfs2(v);
  }
}
void kosarajuExtend(int n) {
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      sub_graph.push_back({});
      dfs(i);
    }
  }
  visited.assign(MAX + 1, 0);
  for (auto &v : sub_graph) {
    dif_topo.push_back({});
    for (auto x : v) {
      if (visited[x] == 0) dfs1(x);
    }
  }
  visited.assign(MAX + 1, 0);
  for (auto &v : dif_topo) {
    scc.push_back({});
    for (int i = (int)v.size() - 1; i >= 0; i--) {
      if (visited[v[i]] == 0) {
        scc.back().push_back(0);
        dfs2(v[i]);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    rev_adj[v].push_back(u);
    undirected[u].push_back(v);
    undirected[v].push_back(u);
    visited[u] = 0, visited[v] = 0;
  }
  kosarajuExtend(n);
  long long res = 0;
  for (auto &v : scc) {
    int sum = 0;
    for (auto x : v) {
      sum += x;
    }
    if (sum == v.size())
      res += sum - 1;
    else
      res += sum;
  }
  cout << res;
}
