import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### # 強連結成分分解(SCC): グラフGに対するSCCを行う # 入力: : 頂点サイズ, : 順方向の有向グラフ, : 逆方向の有向グラフ # 出力: (<ラベル数>, <各頂点のラベル番号>) def scc(N, G, RG, y): order = [] used = [0]*N group = [None]*N def dfs(s): used[s] = 1 for t,x in G[s]: if x > y: continue if not used[t]: dfs(t) order.append(s) def rdfs(s, col): group[s] = col used[s] = 1 for t, x in RG[s]: if x > y: continue if not used[t]: rdfs(t, col) for i in range(N): if not used[i]: dfs(i) used = [0]*N label = 0 for s in reversed(order): if not used[s]: rdfs(s, label) label += 1 return label, group # 縮約後のグラフを構築 def construct(N, G, label, group): G0 = [set() for i in range(label)] GP = [[] for i in range(label)] for v in range(N): lbs = group[v] for w in G[v]: lbt = group[w] if lbs == lbt: continue G0[lbs].add(lbt) GP[lbs].append(v) return G0, GP n,q = na() g = [[] for i in range(n)] rg = [[] for i in range(n)] for j in range(q): a,b = na() a-=1 b-=1 g[a].append((b,j)) rg[b].append((a, j)) ok = q ng = -1 while (ok-ng)>1: d = (ok+ng)//2 if scc(n,g,rg,d)[0]