Robot Rapping Results Report

While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level.

Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level.

Input

The first line of the input consists of two integers, the number of robots n (2 ≤ n ≤ 100 000) and the number of rap battles m với .

The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers u i and v i (1 ≤ u i, v i ≤ nu i ≠ v i), indicating that robot u i beat robot v i in the i-th rap battle. No two rap battles involve the same pair of robots.

It is guaranteed that at least one ordering of the robots satisfies all m relations.

Output

Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1.

Examples

input

4 52 11 32 34 24 3

output

4

input

3 21 23 2

output

-1

Note

In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles.

In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles.

Solution:

#include <bits/stdc++.h>

using namespace std;

const int N = 1234567;

vector <int> g[N];
int qs[N], qf[N], deg[N], x[N];

int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", qs + i, qf + i);
qs[i]--; qf[i]--;
}
int low = 0, high = m + 1;
while (low < high) {
int mid = (low + high) >> 1;
for (int i = 0; i < n; i++) {
g[i].clear();
deg[i] = 0;
}
for (int j = 0; j < mid; j++) {
g[qs[j]].push_back(qf[j]);
deg[qf[j]]++;
}
int e = 0;
for (int i = 0; i < n; i++) {
if (deg[i] == 0) {
x[e++] = i;
}
}
int b = 0;
while (b < e) {
int who = x[b];
for (int j = 0; j < (int) g[who].size(); j++) {
int u = g[who][j];
deg[u]--;
if (deg[u] == 0) {
x[e++] = u;
}
}
b++;
}
bool ok = true;
for (int i = 0; i < e - 1; i++) {
int u = x[i];
int v = x[i + 1];
bool has = false;
for (int j = 0; j < (int) g[u].size(); j++) {
if (g[u][j] == v) {
has = true;
break;
}
}
if (!has) {
ok = false;
break;
}
}
if (ok) {
high = mid;
} else {
low = mid + 1;
}
}
printf("%d\n", low <= m ? low : -1);
return 0;
}