n,m = map(int,input().split())
ls = []
for i in range(m):
    ls.append(list(map(int,input().split())))
ans = []
for i in range(n):
    ans.append(0)

t = 0
for i in range(m):
    if ans[ls[i][0]-1] == 0:
        ans[ls[i][0]-1] = ls[i][1]
        t += 1
    elif ans[ls[i][0]-1] != ls[i][1]:
        print(-1)
        break

d = []
if t == m:
    for i in range(n):
        d.append(str(ans[i]))
    l = sum(d)
    print(l)