#include<iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

vector<int> g[10000];
int nex[10000]={0};
int ans=0;



void topogi(int i){
	nex[i]=1;
	for(int j=0;j<g[i].size();j++){
		if(nex[g[i][j]]==0){
			topogi(g[i][j]);
		}
		else if(nex[g[i][j]]==1){
			ans=1;
		}
	}
	nex[i]=2;
}
int main(){
	int v,e,s,t;
	int temp;
	
	cin>>v>>e;
	for(int i=0;i<e;i++){
		cin>>s>>t;
		g[s].push_back(t);
	}
	for(int i=0;i<v;i++){
		if(nex[i]==0){
			topogi(i);
		}
	}

	/*while(!A.empty()){
		temp=A.front();
		A.pop();
		printf("%d\n",temp);
		for(int i=0;i<g[temp].size();i++){
			next[g[temp][i]]--;
			if(next[g[temp][i]]==0)A.push(g[temp][i]);
		}
	}*/
	cout<<ans<<endl;
	return 0;
}




