#include <iostream>
#include <sstream>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<bool> f;
vector<vector<int> > g;
bool dfs(int v,int s){
  f[v] = true;
  //cout<<v<<" g:"<<g[v][0]<<" s:"<<s<<endl;
  //cout<<g[v].size()<<endl;
  for(auto i : g[v]){
    //cout<<v<<" i:"<<i<<" s:"<<s<<endl;
    if(s==i) return 1;
    //cout<<v<<" i:"<<i<<" s:"<<s<<"....2"<<endl;
    if(f[i])continue;
    if(dfs(i,s))return 1;
  }

  return 0;
}
int main(){
  int v,e,s,t;
  cin>>v>>e;
  g.resize(v);
  f.resize(v);
  bool flag =0;
  for(int i=0;i<e;i++){
    cin>>s>>t;
    g[s].push_back(t);
  }
  for(int i=0; i<v;i++){
    if(g[i].empty())continue;
    flag=dfs(i,i);
    //cout<<flag<<endl;
    if(flag)break;
    for(int i=0;i<v;i++)f[i]=false;
  }
  cout<<flag<<endl;

  return 0;
}

