#include<iostream>
#include<cstring>
#include<queue>
#include<cctype>
using namespace std;
inline long long read(){
long long x=0;char c=getchar();bool f=0;
while(!isdigit(c)) f|=c=='-',c=getchar();
while(isdigit(c)) x=x*10+(c^48),c=getchar();
return f?-x:x;
}
int dfn[50010],c[50010],f[50010],t,sd[50010],ans[50010],low[50010],top,stac[50010],n,m,to[50010],nxt[50010],h[50010],cnt;
bool vis[50010];
void add(int u,int v){
to[++cnt]=v;
f[cnt]=u;
nxt[cnt]=h[u];
h[u]=cnt;
}
void tarjan(int x){
dfn[x]=low[x]=++cnt;
stac[++top]=x;vis[x]=1;
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(!dfn[y]){
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(vis[y]) {
low[x]=min(dfn[y],low[x]);
}
}
if(dfn[x]==low[x]){
int y;++t;
while(y=stac[top--]){
vis[y]=0;
sd[y]=t;
ans[t]++;
if(x==y) break;
}
}
}
int main(){
n=read(),m=read();
for(int a,b,i=1;i<=m;i++){
a=read(),b=read();
add(a,b);
}
cnt=0;
for(int i=1;i<n;i++){
if(!dfn[i]) tarjan(i);
}
cnt=0;
memset(h,0,sizeof(h));
for(int i=1;i<=m;i++){
int x=sd[f[i]],y=sd[to[i]];
if(x!=y){
add(x,y);
c[x]++;
}
}
int sum=0;
int o;
for(int i=1;i<=t;i++){
if(!c[i]){
sum++;
o=ans[i];
if(sum==2){
o=0;
break;
}
}
}
cout<<o;
}