import sys input = sys.stdin.readline from collections import defaultdict def main(): n,k = map(int,input().split()); MOD = pow(10,9) + 7 if k == 0: print(1);exit() #A = [[0]*n for _ in range(n)] G = [[] for _ in range(n)] deg = [0]*n dic = {} for _ in range(k): r,c = map(int,input().split()) r -= 1; c -= 1 if r == c: #対角成分に1があったら無理 print(-1);exit() G[r].append(c) deg[c] += 1 TP = topological(G,deg) #print(TP) if len(TP) < n: print(-1);exit() dp = [0]*n for i in range(n): if deg[i] == 0: dp[i] = 1 for v in TP: for u in G[v]: dp[u] = max(dp[u], dp[v] + 1) #print(dp) ans = max(dp) print(ans) # degは入次数を記録したもの def topological(graph, deg): start = [] n = len(deg) for i in range(n): if deg[i] == 0: #入次数がないものがスタート start.append(i) topo = [] while start: v = start.pop() topo.append(v) for u in graph[v]: deg[u] -= 1 if deg[u] == 0: start.append(u) #トポロジカルソートできない場合は配列がnよりも短くなる。 return topo if __name__ == "__main__": main()