N,K=map(int,input().split()) graph=[[] for x in range(N)] degree=[0]*N for k in range(K): r,c=map(int,input().split()) r-=1;c-=1 graph[r].append(c) degree[c]+=1 queue=[] inf=1<<30 dp=[-inf]*N for x in range(N): if degree[x]==0: queue.append(x) dp[x]=0 while queue: x=queue.pop() for y in graph[x]: dp[y]=max(dp[y],dp[x]+1) degree[y]-=1 if degree[y]==0: queue.append(y) if sum(degree): ans=-1 else: ans=max(dp)+1 print(ans)