#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = { 0, 1, 0, -1 }, dj[] = { 1, 0, -1, 0 };
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define MAX 100
#define vmax 10000
#define sentinel 2000000000
#define INF (1<<30)
constexpr long long int inf = 100000000000000000;
constexpr long long int rinf = -(INT64_MAX);
static const int NIL=-1;

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

struct edge{
    int f,t,c;
};

int d[MAX];
vector<edge> g;

int main(){
    int v,e,s,t;
    cin>>v>>e;
    for(int i=0;i<e;i++){
        cin>>s>>t;
        g.push_back({s,t,-1});
    }
    for(int i=0;i<v;i++){
        d[i]=0;
    }
    for(int i=0;i<v;i++){
        for(auto e:g){
            if(d[e.t]>d[e.f]+e.c){
                if(i==v-1){
                    cout<<1<<endl;
                    return 0;
                }
                d[e.t]=d[e.f]+e.c;
            }
        }
    }
    cout<<0<<endl;
    return 0;
}




