N, M = map(int, input().split())
for _ in range(M):
    s, c = map(int, input().split())
    
int l=0

for i in range(N)
    for j in range(N)
        if s[i]==s[j] and c[i]=!c[j]:
            print(str(-1))
            return
for i in range(N)
    l+=s[i]*(N-i+1)