#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
using namespace std;
typedef long long LL;
const int INF = 999999999;

struct edge
{
  int src, to, cost;
};
using Edges = vector<edge>;
using WeightedGraph = vector<Edges>;

vector<int> tsort(WeightedGraph &G){
    vector<int> tsorted;
    vector<int> used(G.size(), 0);
    bool f = false;
    function<void(int)> dfs = [&](int u){
        if(used[u] > 0){
            if(used[u] == 1) f = true;
            return;
        }
        used[u] = 1;
        for(auto &e : G[u]) dfs(e.to);
        used[u] = 2;
        tsorted.pb(u);
    };
    rep(i, G.size()) dfs(i);
    if(f) return vector<int>();
    reverse(all(tsorted));
    return tsorted;
}

int main()
{
  int n, m;
  cin >> n >> m;
  WeightedGraph G(n);
  rep(i, m)
  {
    int s, t;
    cin >> s >> t;
    G[s].emplace_back((edge){s, t, 1});
  }
  vector<int> ans = tsort(G);
  cout << (ans.size() == 0 ? 1:0) << endl;
}

