#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 5e3 + 10, M = 5e5 + 10;
int h[N], ne[M], e[M], idx;
int n, m, res;
bool father[N];
bool son[N];
int target;
void add (int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u)
{
for (int i=h[u]; ~i; i = ne[i])
{
int v = e[i];
dfs(v);
}
if (target == u)
res ++;
return ;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof(h));
while (m --)
{
int a, b;
cin >> a >> b;
add (b, a);
father[a] = true;
son[b] = true;
}
int root = 1;
while (father[root]) root ++;
target=1;
while (son[target]) target ++;
dfs (root);
cout << res << endl;
return 0;
}