#include "bits/stdc++.h"
#define rep(i, s, n)    for (int i = (s); i < (n); ++i)
using namespace std;

int V, E;
vector<int> G[1005];
enum {
    NEW, ACTIVE, FINISHED
} stat[1005];

bool dfs(int u) {
    stat[u] = ACTIVE;
    for (const int v : G[u]) {
        if (stat[v] == ACTIVE || dfs(v)) return true;
    }
    stat[u] = FINISHED;
    return false;
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    cin >> V >> E;
    rep(i, 0, E) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        G[a].push_back(b);
    }

    rep(u, 0, V) {
        if (stat[u] == NEW && dfs(u)) {
            cout << 0 << endl;
            return 0;
        }
    }

    cout << -1 << endl;

    return 0;
}
