import sys input = sys.stdin.readline N,M=map(int,input().split()) EDGE=[list(map(int,input().split())) for i in range(M)] for i in range(M): EDGE[i][0]-=1 EDGE[i][1]-=1 E=[[] for i in range(N)] for i in range(M): x,y=EDGE[i] if x==0 and y==N-1: print(-1) exit() if x==N-1 and y==0: print(-1) exit() E[x].append((y,i)) E[y].append((x,i)) if len(E[0])<=M//2: ANS=["R"]*M NOW=0 Q=[0] USE=[0]*N USE[0]=1 while Q: x=Q.pop() for to,ind in E[x]: if to==N-1: continue if ANS[ind]=="R" and NOW