645_D. 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 (<image>).

The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), indicating that robot ui beat robot vi 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 5
2 1
1 3
2 3
4 2
4 3


Output

4


Input

3 2
1 2
3 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.
```python
from collections import defaultdict


class RobotRapping():
    def __init__(self, n, m, battles):
        self.n, self.m = n, m
        self.battles = battles

    def generate_graph(self, k):
        edge_map = defaultdict(list)
        rev_map = defaultdict(list)
        for i in range(k):
            edge_map[self.battles[i][0]-1].append((self.battles[i][1]-1, i))
            rev_map[self.battles[i][1]-1].append((self.battles[i][0]-1, i))
        return edge_map, rev_map

    def check_order(self, num_battles):
        edge_map, rev_map = self.generate_graph(num_battles)
        outgoing_cnt = defaultdict(int)
        for k in edge_map:
            outgoing_cnt[k] = len(edge_map[k])
        s = []
        cntr = 0
        for i in range(self.n):
            if outgoing_cnt[i] == 0:
                s.append(i)
        while len(s) > cntr:
            if len(s) > cntr+1 :
                return False
            else:
                node = s[cntr]
                for v in rev_map[node]:
                    outgoing_cnt[v] -= 1
                    if outgoing_cnt[v] == 0:
                        s.append(v)
                cntr += 1
        return True

    def min_battles(self):
        if not self.check_order(self.m):
            print(-1)
        else:
            mn, mx = 0, self.m
            while mn < mx-1:
                md = int((mn+mx)/2)
                if self.check_order(md):
                    mx = md
                else:
                    mn = md
            print(mx)


    def min_battles2(self):
        edge_map, rev_map = self.generate_graph(self.m)
        outgoing_cnt = defaultdict(int)
        for k in edge_map:
            outgoing_cnt[k] = len(edge_map[k])
        s = []
        cntr = 0
        order = []
        for i in range(self.n):
            if outgoing_cnt[i] == 0:
                s.append(i)
        while len(s) > cntr:
            if len(s) > cntr+1 :
                print(-1)
                return
            else:
                node = s[cntr]
                order.append(node)
                for v,_ in rev_map[node]:
                    outgoing_cnt[v] -= 1
                    if outgoing_cnt[v] == 0:
                        s.append(v)
                cntr += 1
        mn_pos = -1
        for i in range(1,self.n):
            for v,ind in edge_map[order[i]]:
                if v == order[i-1]:
                    mn_pos = max(mn_pos, ind)
                    break
        print(mn_pos+1)

n,m = list(map(int,input().strip(' ').split(' ')))
battles = []
for i in range(m):
    x,y = list(map(int,input().strip(' ').split(' ')))
    battles.append((x,y))
rr = RobotRapping(n,m,battles)
rr.min_battles2()

```