#include <bits/stdc++.h>
void fre() {
  freopen("c://test//input.in", "r", stdin);
  freopen("c://test//output.out", "w", stdout);
}
template <class T>
inline void gmax(T &a, T b) {
  if (b > a) a = b;
}
template <class T>
inline void gmin(T &a, T b) {
  if (b < a) a = b;
}
using namespace std;
const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, maxint = 2147483647,
          ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double eps = 1e-8, PI = acos(-1.0);
int n, m;
int ind[N];
int sta[N], top;
vector<int> a[N];
vector<int> b[N];
bool vis[N];
int num;
void dfs(int x) {
  vis[x] = 1;
  ++num;
  if (ind[x] == 0) sta[++top] = x;
  for (auto y : a[x])
    if (!vis[y]) dfs(y);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; i++)
      a[i].clear(), b[i].clear(), ind[i] = vis[i] = 0;
    for (int i = 1; i <= m; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      ++ind[y];
      a[x].push_back(y);
      a[y].push_back(x);
      b[x].push_back(y);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
      if (!vis[i]) {
        num = top = 0;
        dfs(i);
        int sum = top;
        while (top) {
          int x = sta[top--];
          for (auto y : b[x]) {
            if (--ind[y] == 0) {
              sta[++top] = y;
              ++sum;
            }
          }
        }
        if (sum == num)
          ans += (num - 1);
        else
          ans += num;
      }
    printf("%d\n", ans);
  }
  return 0;
}
