#include<bits/stdc++.h>
using namespace std;
typedef long long ll;


int n,m;
vector<vector<int>> v(100010);

bool used[100010];

bool iscycle(int p,vector<int> went){
    used[p] = 1;
    bool ret = false;
    went[p] = 1;
        
    for(auto i:v[p]){
        if(!used[i]){
            ret |= iscycle(i,went);
        }
        if(went[i]) ret = 1;
    }
    return ret;
}

signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    cout << fixed << setprecision(20);

    cin>>n>>m;
    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        v[a].push_back(b);
    }    
    for(int i=0;i<n;i++){
        vector<int> wet(100010);
        if(!used[i]){
            if(iscycle(i,wet)){
                cout<<1<<endl;
                return 0;
            }
        }
    }
    cout<<0<<endl;
}
