#include namespace { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic pop using namespace std; using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) { a = max(a, b); } template void chmin(T& a, const T& b) { a = min(a, b); } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; bool check(const VVI& to) { const int n = to.size(); vector d(n); vector visited(n); rep(i, n) if (!visited[i]) { auto dfs = [&](auto&& self, int u) { if (d[u]) return true; if (visited[u]) return false; d[u] = true; visited[u] = true; for(int v: to[u]) { if (self(self, v)) return true; } d[u] = false; return false; }; if (dfs(dfs, i)) return true; } return false; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; VI a(q), b(q); rep(i, q) cin >> a[i] >> b[i], a[i]--, b[i]--; int l = 0, r = q + 1; VVI to(n); while(r - l > 1) { int c = (l + r) / 2; rep(i, n) to[i].resize(0); rep(i, c) to[a[i]].emplace_back(b[i]); if (check(to)) r = c; else l = c; } if (r == q + 1) r = -1; cout << r << '\n'; }