import math from sys import stdin n, q, *indata = map(int, stdin.read().split()) offset = 0 m = int(math.floor(math.sqrt(q))) g = [[] for i in range(n+1)] repeat = (q-1) // m + 1 cyclecheck = -1 for i in range(repeat): g2 = [[] for i in range(n+1)] for j in range(m): ind = i * m + j if ind >= q: break else: s, t = indata[ind*2]-1,indata[ind*2+1]-1 g2[s].append(t) check = [0 for j in range(n)] for j in range(n): if check[j] == 0: que = [(j,0)] while que: now, inout = que.pop() if inout == 0: if check[now] == 0: que.append((now,1)) check[now] = 1 for k in g[now]: if check[k] == 1: cyclecheck = i break elif check[k] == 0: que.append((k,0)) for k in g2[now]: if check[k] == 1: cyclecheck = i break elif check[k] == 0: que.append((k,0)) if cyclecheck != -1: break else: check[now] = 2 if cyclecheck != -1: break if cyclecheck != -1: break else: for j in range(m): ind = i * m + j if ind >= q: break else: s, t = indata[ind*2]-1,indata[ind*2+1]-1 g[s].append(t) if cyclecheck == -1: print("{}".format(-1)) exit() for i in range(m): ind = cyclecheck * m + i if ind >= q: break else: s, t = indata[ind*2]-1,indata[ind*2+1]-1 g[s].append(t) check = [0 for j in range(n)] for j in range(n): if check[j] == 0: que = [(j,0)] while que: now, inout = que.pop() if inout == 0: if check[now] == 0: que.append((now,1)) check[now] = 1 for k in g[now]: if check[k] == 1: ans = cyclecheck * m + i + 1 print("{}".format(ans)) exit() elif check[k] == 0: que.append((k,0)) else: check[now] = 2