#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 100010;
int n, m, in[MAXN], out[MAXN], ans, f[MAXN];
vector<int> cd[MAXN];
int dfs(int x) {
// cout << x << endl;
if(!out[x]) {
ans ++;
// cout << 1;
f[x] = 1;
return 1;
}
if(f[x]) {
ans += f[x];
return f[x];
}
for(int i = 0; i < cd[x].size(); i ++) {
int u = cd[x][i];
// cout << ' ' << u << endl;
dfs(u);
f[x] += f[u];
}
return f[x];
}
signed main() {
freopen("chain_2016.in", "r", stdin);
freopen("chain_2016.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; i ++) {
int x, y;
cin >> x >> y;
cd[x].push_back(y);
in[y] ++;
out[x] ++;
}
for(int i = 1; i <= n; i ++) {
// cout << out[i] << ' ';
if(in[i] == 0) {
dfs(i);
if(!out[i]) {
ans --;
}
}
}
cout << ans;
return 0;
}