import sys import pypyjit pypyjit.set_param('max_unroll_recursion=-1') sys.setrecursionlimit(200000) n, m = map(int, input().split()) ikeru = [[] for i in range(n)] ikeruinv = [[] for i in range(n)] for j in range(m): a, b = map(int, input().split()) if a == b: continue ikeru[a-1].append((b-1, j + 1)) ikeruinv[b-1].append((a-1, j + 1)) def dfs(a): global ichi if tansaku[a] == 1: return tansaku[a] = 1 for i, v in ikeru[a]: if v <= targ: if tansaku[i] == 0: dfs(i) dat[a] = ichi ichi += 1 return suki = m+3 kirai = -1 while suki - kirai > 1: targ = (suki + kirai) // 2 mode = 1 tansaku = [0 for i in range(n)] tansakuinv = [0 for i in range(n)] dat = [0 for i in range(n)] ichi = 0 for i in range(n): dfs(i) u = [(i, dat[i]) for i in range(n)] u.sort(key=lambda x:-x[1]) for i, d in u: if tansakuinv[i] == 1: continue tansakuinv[i] = 1 for j, v in ikeruinv[i]: if v <= targ: if tansakuinv[j] == 0: mode = 0 break if mode: kirai = targ else: suki = targ if suki > m: print(-1) else: print(suki)