#include <bits/stdc++.h>
using namespace std;
const long long LN = (long long)(5e6), LK = (long long)(1e6),
                INF = (long long)(1e18), MOD = (long long)(1e9) + 7;
long long N, M, ind, ans, tcount, tcomp;
vector<vector<int> > adj;
vector<set<int> > adj1;
struct SNode {
  int disc, low, component;
  bool visited, in_stack;
};
vector<SNode> node;
struct SSCC {
  vector<vector<int> > component;
  stack<int> path;
  int C;
  void build() { component.resize(N); }
  void process(int u) {
    int v = path.top();
    while (v != u) {
      component[C].push_back(v);
      node[v].component = C;
      node[v].in_stack = false;
      path.pop();
      v = path.top();
    }
    component[C].push_back(u);
    node[v].component = C;
    node[u].in_stack = false;
    path.pop();
    ans += component[C].size() - 1;
    C++;
  }
};
SSCC SCC;
void dfs(int u) {
  SCC.path.push(u);
  node[u].in_stack = true;
  node[u].visited = true;
  node[u].disc = node[u].low = ind++;
  for (auto v : adj[u]) {
    if (!node[v].visited) dfs(v);
    if (node[v].in_stack) node[u].low = min(node[u].low, node[v].low);
  }
  if (node[u].disc == node[u].low) SCC.process(u);
}
void dfs1(int u) {
  if (node[u].visited) return;
  node[u].visited = true;
  tcount++;
  tcomp += (SCC.component[u].size() > 1 ? 1 : 0);
  for (auto v : adj1[u]) dfs1(v);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  adj.resize(N);
  adj1.resize(N);
  node.resize(N);
  SCC.build();
  cin >> M;
  for (int i = 0, u, v; i < M; i++) {
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
  }
  for (int u = 0; u < N; u++)
    if (!node[u].visited) dfs(u);
  for (int u = 0; u < N; u++) {
    node[u].visited = false;
    for (auto v : adj[u])
      if (node[u].component != node[v].component &&
          !adj1[node[u].component].count(node[v].component))
        adj1[node[u].component].insert(node[v].component),
            adj1[node[v].component].insert(node[u].component);
  }
  for (int u = 0; u < N; u++, tcomp = tcount = 0) {
    dfs1(u);
    ans += (tcount ? tcount - 1 : 0);
    ans += (tcomp ? 1 : 0);
  }
  cout << ans;
  return 0;
}
