#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 x, y;
int ind[N], s[N], num, top;
vector<int> a[N], b[N];
bool e[N];
void dfs(int x) {
  e[x] = 1;
  num++;
  if (ind[x] == 0) s[++top] = x;
  for (int i = a[x].size() - 1; i >= 0; i--) {
    int y = a[x][i];
    if (!e[y]) dfs(y);
  }
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(ind, 0, sizeof(ind));
    for (int i = 1; i <= n; i++) a[i].clear(), b[i].clear();
    for (int i = 1; i <= m; i++) {
      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;
    memset(e, 0, sizeof(e));
    for (int i = 1; i <= n; i++)
      if (!e[i]) {
        num = top = 0;
        dfs(i);
        int sum = top;
        while (top) {
          int x = s[top--];
          for (int j = b[x].size() - 1; j >= 0; j--) {
            int y = b[x][j];
            ind[y]--;
            if (ind[y] == 0) {
              s[++top] = y;
              sum++;
            }
          }
        }
        if (sum == num)
          ans += (num - 1);
        else
          ans += num;
      }
    printf("%d\n", ans);
  }
  return 0;
}
