#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>

using Graph = std::vector<std::vector<int>>;

std::vector<bool> visited;  // visited.assign(n, false);
std::vector<bool> finished; // finished.assign(n, false);
std::stack<int> history;

int dfs(const Graph &g, const int v) {
  visited[v] = true;
  history.push(v);
  for (auto w : g[v]) {
    if (finished[w])
      continue;

    if (visited[w])
      return w;

    const int res = dfs(g, w);
    if (res != -1)
      return res;
  }
  history.pop();
  finished[v] = true;
  return -1;
}

std::vector<int> detect_cycle(const Graph &g) {
  for (int s = 0; s < (int)g.size(); ++s) {
    if (visited[s])
      continue;

    const int res = dfs(g, s);
    if (res != -1) {
      std::vector<int> cycle;
      while (not history.empty()) {
        int v = history.top();
        history.pop();
        cycle.push_back(v);
        if (v == res)
          break;
      }
      std::reverse(cycle.begin(), cycle.end());
      return cycle;
    }
  }
  return std::vector<int>();
}

int main() {
  int n, m;
  std::cin >> n >> m;

  Graph g(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    std::cin >> s >> t;
    g[s].push_back(t);
  }

  visited.assign(n, false);
  finished.assign(n, false);

  auto res = detect_cycle(g);

  if (res.size() >= 1)
    std::cout << 1 << std::endl;
  else
    std::cout << 0 << std::endl;

  return 0;
}

