#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll read(){
ll X = 0 ; int f = 1;
char ch = getchar();
while(!isdigit(ch) && ch != '-') ch = getchar();
if(ch == '-'){
ch = getchar();
f = -1;
}
while(isdigit(ch)){
X = (X<<1)+(X<<3)+ch-'0';
ch = getchar();
}
return X*f;
}
const int N = 1e4+10 , M = 5e4+10;
int n,m;
int to[M] , nxt[M] , head[N] , tot;
inline void add(int x,int y){
to[++tot] = y;
nxt[tot] = head[x];
head[x] = tot;
}
int dfn[N],low[N],c[N],Num,Sum,ans[N];
int s[N],top; bool ins[N];
void tarjan(int x){
dfn[x] = low[x] = ++Num;
s[++top] = x; ins[x] = true;
for(int i=head[x];i;i=nxt[i]){
int y = to[i];
if(!dfn[y]){
tarjan(y);
low[x] = min(low[x],low[y]);
}else if(!ins[y])
low[x] = min(low[x],dfn[y]);
}
if(dfn[x] == low[x]){
++Sum;
int k;
do{
k = s[top]; top--;
ins[k] = false;
c[k] = Sum;
ans[Sum]++;
}while(k != x);
}
}
int out[N];
int main(){
n = read(); m = read();
for(int i=1;i<=m;i++){
int a = read() , b = read();
add(a,b);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int u=1;u<=n;u++)
for(int j=head[u];j;j=nxt[j]){
int v = to[j];
if(c[u] != c[v]) out[c[u]]++;
}
int cnt = 0;
for(int i=1;i<=Sum;i++)
if(!out[i]){
if(cnt){
cout << 0;
return 0;
}
cnt = i;
}
cout << ans[cnt];
return 0;
}