import sys input = sys.stdin.readline from collections import defaultdict def main(): n,k = map(int,input().split()) if k == 0: print(1);exit() #A = [[0]*n for _ in range(n)] dic = {} for _ in range(k): r,c = map(int,input().split()) r -= 1; c -= 1 if r == c: #対角成分に1があったら無理 print(-1);exit() if r not in dic: dic[r] = [c] else: dic[r].append(c) #print(dic) for i in range(1,2*pow(10,5)+100): if not dic: print(i);exit() predic = {} dic,predic = predic,dic for r in predic: for nr in predic[r]: if nr not in predic: continue nc = predic[nr] if r in nc: print(-1);exit() dic[r] = nc #print(dic) if __name__ == "__main__": main()