#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> G[100005];
vector<int> GG[100005];
vector<int> Area[100005];
int Sort[100005], number, cycle;
int Poz[100005], area;
bool Use[100005];
void Read() {
  cin >> N >> M;
  for (int i = 1; i <= M; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    GG[x].push_back(y);
    GG[y].push_back(x);
  }
}
void DFS2(int node) {
  Use[node] = 1;
  Area[area].push_back(node);
  for (int i = 0; i < GG[node].size(); i++) {
    int neighb = GG[node][i];
    if (Use[neighb] == 0) DFS2(neighb);
  }
}
void DFS(int node) {
  ++number;
  Use[node] = 1;
  for (int i = 0; i < G[node].size(); i++) {
    int neighb = G[node][i];
    if (Use[neighb] == 0) {
      DFS(neighb);
    } else if (Poz[neighb] == 0)
      cycle = 1;
  }
  Sort[++Sort[0]] = node;
  Poz[node] = Sort[0];
}
void precalcArea() {
  for (int i = 1; i <= N; i++)
    if (Use[i] == 0) {
      ++area;
      DFS2(i);
    }
  memset(Use, 0, sizeof(Use));
}
void Browse() {
  int i, result = 0;
  for (i = 1; i <= area; i++) {
    for (int j = 0; j < Area[i].size(); j++)
      if (Use[Area[i][j]] == 0) DFS(Area[i][j]);
    result += cycle + Area[i].size() - 1;
    number = 0;
    Sort[0] = 0;
    cycle = 0;
  }
  cout << result << "\n";
}
int main() {
  Read();
  precalcArea();
  Browse();
  return 0;
}
