#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  T r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
int getbit(T s, int i) {
  return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
  return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
  return s & (~(T(1) << i));
}
const double PI = 2 * acos(0.0);
const long double eps = 1e-12;
const int infi = 1e9;
const long long Linfi = (long long)9e18;
const long long MOD = 1000000007;
int n, m, u, v, dem, cnt = 0;
int List[100005], group[100005], xet[100005], inDFS[100005];
vector<int> adj[100005], adj2[100005], V[100005];
vector<pair<int, int> > GE[100005];
pair<int, int> E[100005];
set<pair<int, int> > S;
void DFS(int u) {
  group[u] = cnt;
  V[cnt].push_back(u);
  xet[u] = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (xet[v] == 0) DFS(v);
  }
}
void visit(int u, int &ok) {
  xet[u] = 1;
  inDFS[u] = 1;
  for (int i = 0; i < adj2[u].size(); i++) {
    int v = adj2[u][i];
    if (xet[v] == 0)
      visit(v, ok);
    else if (xet[v] == 1 && inDFS[v])
      ok = 1;
  }
  inDFS[u] = 0;
}
void solve() {
  for (int i = 1; i <= n; i++)
    if (xet[i] == 0) {
      cnt++;
      DFS(i);
    }
  for (int i = 1; i <= m; i++) {
    int u = E[i].first;
    int v = E[i].second;
    int g = group[u];
    GE[g].push_back(E[i]);
  }
  int ans = 0;
  memset(xet, 0, sizeof(xet));
  for (int i = 1; i <= cnt; i++) {
    int ok = 0;
    for (int j = 0; j < V[i].size(); j++) {
      int u = V[i][j];
      if (xet[u] == 0) visit(u, ok);
    }
    if (ok)
      ans += V[i].size();
    else
      ans += V[i].size() - 1;
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    adj2[u].push_back(v);
    adj[u].push_back(v);
    adj[v].push_back(u);
    E[i] = pair<int, int>(u, v);
    S.insert(pair<int, int>(u, v));
  }
  solve();
  return 0;
}
