N,M = map(int, input().split())
Si = []
Ci = []
check1 = 0
check2 = 0
check3 = 0
lastcheck = 0
sum = 0
for i in range(M):
    S,C = map(int, input().split())
    Si.append(S)
    Ci.append(C)
if N==1:
    for i in range (M):
        sum +=C[i]
        check3  = i
        if C[i] != C[check3]:
            lastcheck=0
elif N==2:
    for i in range (M):
        if Si[i] == 2:
            sum +=C[i]*10
            check2 = i
            if C[i] != C[check2]:
                lastcheck=0
        else:
            sum +=C[i]
            check3=i
            if C[i] != C[check3]:
                lastcheck=0
else:
    for i in range (M):
        if Si[i] == 1:
            sum+=int(Ci[i]*100)
            check1 = i
            if int(C[i]) != int(C[check1]):
                lastcheck=0
        elif Si[i] == 2:
            sum +=C[i]*10
            check2 = i
            if C[i] != C[check2]:
                lastcheck=0
        else:
            sum +=C[i]
            check3=i
            if C[i] != C[check3]:
                lastcheck=0
if lastcheck == 0:
    print('-1')
else:
    print(sum)
            