#include <bits/stdc++.h>
using namespace std;
void RI(int& x) {
  x = 0;
  char c = getchar();
  while (!(c >= '0' && c <= '9' || c == '-')) c = getchar();
  bool flag = 1;
  if (c == '-') {
    flag = 0;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (!flag) x = -x;
}
void RII(int& x, int& y) { RI(x), RI(y); }
void RIII(int& x, int& y, int& z) { RI(x), RI(y), RI(z); }
void RC(char& c) {
  c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
}
char RC() {
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  return c;
}
const long long mod = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 1e9;
const double EPS = 1e-8;
const int Maxn = 300000, Maxm = 300000;
int n, m, u, v, i, ans, tot, e0, num, t, s[Maxn], dfn[Maxn], low[Maxn],
    val[Maxn], top[Maxn], col[Maxn], e[Maxm * 3][3];
bool bo[Maxn], vis[Maxn], instack[Maxn];
void build(int u, int v) {
  e[++e0][0] = v;
  e[e0][1] = top[u];
  top[u] = e0;
}
void dfs(int u) {
  dfn[u] = low[u] = t++;
  vis[u] = instack[u] = 1;
  s[++s[0]] = u;
  int p = top[u];
  while (p) {
    int v = e[p][0];
    if (!vis[v])
      dfs(v), low[u] = min(low[u], low[v]);
    else if (instack[v])
      low[u] = min(low[u], dfn[v]);
    p = e[p][1];
  }
  if (dfn[u] == low[u]) {
    num++;
    do {
      val[num]++;
      p = s[s[0]--];
      col[p] = num;
      instack[p] = 0;
    } while (p != u);
  }
}
map<pair<int, int>, int> ck;
int cnt[Maxn], Yes[Maxn];
struct edge {
  int v, nt;
} e2[Maxm];
int e02 = 0, top2[Maxn];
void build2(int u, int v) {
  e2[++e02].v = v;
  e2[e02].nt = top2[u];
  top2[u] = e02;
}
int f[Maxn], sum[Maxn];
int gf(int x) {
  if (x == f[x]) return f[x];
  return f[x] = gf(f[x]);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    Yes[u] = Yes[v] = 1;
    build(u, v);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  for (int i = 1; i <= n; i++)
    if (Yes[i]) cnt[col[i]]++;
  for (int i = 1; i <= num; i++) f[i] = i;
  for (int u = 1; u <= n; u++) {
    for (int p = top[u]; p; p = e[p][1]) {
      int v = e[p][0];
      int x = col[u], y = col[v];
      if (x != y) {
        x = gf(x);
        y = gf(y);
        f[x] = y;
      }
    }
  }
  memset(sum, 0, sizeof(sum));
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= num; i++) {
    sum[gf(i)] += cnt[i];
    if (cnt[i] > 1) vis[gf(i)] = 1;
  }
  for (int i = 1; i <= num; i++) {
    if (sum[i]) {
      if (vis[i])
        ans += sum[i];
      else
        ans += sum[i] - 1;
    }
  }
  cout << ans << endl;
}
