#include<stdio.h>
#include<stdlib.h>
#define MAXV 100
#define MAXE 1000

int v_num,e_num,f=0;
int *visited;
int *s,*t;

void closedsearch(int);
int main(){
  int i; 
  scanf("%d %d",&v_num,&e_num);
  if(e_num < 0 || MAXE < e_num || v_num < 1 || MAXV < v_num ) return 0;
  s = (int*)malloc(e_num*sizeof(int));
  t = (int*)malloc(e_num*sizeof(int));
  visited = (int*)malloc(v_num*sizeof(int));
  for(i = 0;i<e_num;i++) scanf("%d %d",&s[i],&t[i]);
  for(i = 0;i<v_num;i++) visited[i] = 0;
  for(i = 0;i<v_num;i++){
    if(f == 1) break;
    if(visited[i] == 0 && f == 0) closedsearch(i);
  }
  printf("%d\n",f);
  free(s);
  free(t);
  free(visited);
  return 0;
}

void closedsearch(int v){
  int i;
  if(visited[v] == 1){
    f = 1;
  }else if(visited[v] == 0){
    visited[v] = 1;
    for(i = 0; i < e_num; i++)
      if(s[i]==v) closedsearch(t[i]);
    visited[v] = 2;
  }
}
      

