#include using namespace std; class GraphCycle { private: const vector>& G; std::vector visited, seen; bool result; bool dfs(int v) { if (seen[v]) return true; if (visited[v]) return false; visited[v] = seen[v] = true; for (const auto& e : G[v]) { if (dfs(e)) return true; } seen[v] = false; return false; } void init() { const int N = G.size(); visited.assign(N, false); seen.assign(N, false); result = false; for (int i = 0; i < N; ++i) { if (dfs(i)) { result = true; break; } } } public: GraphCycle(const vector>& G) : G(G) { init(); } bool has_cycle() const { return result; } }; int main() { int N, Q; cin >> N >> Q; vector> G(N); for (int i = 0; i < Q; ++i) { int a, b; cin >> a >> b; G[a - 1].push_back(b - 1); GraphCycle C(G); if (C.has_cycle()) { cout << i + 1 << endl; return 0; } } puts("-1"); }