import sys

N, M = map(int, input().split())
 
s_lis = []
c_lis = []
 
for _ in range(M):
    s, c = map(int, input().split())
    s_lis.append(s)
    c_lis.append(c)

if N not in s_lis:
    print(-1)
    sys.exit()

 


for k in range(N):

    dup_lis = ([i for i, x in enumerate(s_lis) if x == k])
    
    if dup_lis != 0:
        for j in dup_lis:
            if c_lis[dup_lis[0]] != c_lis[j]:
                print(-1)
                sys.exit()
           