Under construction

Still being built. Things may change

Operation Permutation

Interview

Time limit1sMemory limit1024 MB

Summary
Find the smallest number of reported pairs after which the order of soldiers is uniquely determined, or -1 if it never becomes unique.
Level

Medium5 of 10

Topics
Topological sort, Graph, Binary search
Solved
No attempts yet

Problem

General Petrov loves drill practice more than anything. Once, nn soldiers, numbered from 11 to nn for simplicity, took part in a formation. The soldiers lined up in a row, and the soldier at position ii had number aia_i. Afterward, the general's adjutant walked along the line and confirmed that the order matched the one the general had chosen.

Some time later, the general wanted to arrange the soldiers in the same order again. However, he forgot how he had arranged them the first time. Fortunately, the adjutant has a good memory: for mm pairs of positions xi,yix_i, y_i, he remembers that the number of the soldier at position xix_i is less than the number of the soldier at position yiy_i.

The adjutant reports the pairs xi,yix_i, y_i one at a time. The general wants to start arranging the soldiers quickly, though. Find the minimum kk such that, once the adjutant has reported the first kk pairs, the general can uniquely determine the desired permutation.

Input

The first line contains two integers nn and mm: the number of soldiers in the operation and the number of position pairs the adjutant remembers (2≤n≤1052 \le n \le 10^5; 1≤m≤1051 \le m \le 10^5).

Each of the next mm lines contains a pair xi,yix_i, y_i, in the order the adjutant reports them. It means the soldier number at position xix_i is less than the soldier number at position yiy_i (1≤xi,yi≤n1 \le x_i, y_i \le n; xi≠yix_i \ne y_i).

Each pair appears at most once. The input is guaranteed to be consistent: at least one permutation satisfies all the remembered conditions.

Output

Print one integer kk, the smallest index of a pair after which the permutation can be uniquely restored. If the permutation cannot be uniquely restored, print −1-1.

Hint

In the first example, the soldiers stood in the order (5,2,1,3,4)(5, 2, 1, 3, 4). The order can be restored already after the fourth remembered pair.

In the second example, four arrangements satisfy the input: (1,3,4,2)(1, 3, 4, 2), (2,3,4,1)(2, 3, 4, 1), (3,2,4,1)(3, 2, 4, 1), and (4,2,3,1)(4, 2, 3, 1).

Examples2

  1. Example 1

    Input
    5 5
    5 1
    3 2
    2 4
    4 5
    3 5
    
    Expected output
    4
    
  2. Example 2

    Input
    4 2
    4 2
    2 3
    
    Expected output
    -1