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

### FIXED CODE ###

N,M = map(int, input().split())
Si = [0]*M
Ci = [0]*M
lastcheck = 1
precheck = 0
sum = 0
for i in range(M):
    Si[i], Ci[i] = map(int, input().split())
if N==1:
    for i in range (M):
        sum +=Ci[i]
        if Si[i]==1 and Ci[i]==0:
            sum =0
        for j in range(i+1,M):
            if Si[i]==Si[j]:
                sum-=int(Ci[i])
                if Ci[i] != Ci[j]:
                    lastcheck=0
elif N==2:
    for i in range (M):
        if Si[i] == 1:
            if Si[i]==1 and Ci[i]==0:
                lastcheck=0
            sum +=Ci[i]*10
            for j in range(i+1,M):
                if Si[i]==Si[j]:
                    sum-=int(Ci[i]*10)
                    if Ci[i] != Ci[j]:
                        lastcheck=0
        else:
            sum +=Ci[i]
            for j in range(i+1,M):
                if Si[i] == Si[j]:
                    sum-=int(Ci[i])
                    if Ci[i] != Ci[j]:
                        lastcheck=0
else:
    for i in range (M):
        if Si[i] == 1:
            if Si[i]==1 and Ci[i]==0:
                lastcheck=0
            sum+=int(Ci[i]*100)
            for j in range(i+1,M):
                if Si[i]==Si[j]:
                    sum-=int(Ci[i]*100)
                    if Ci[i] != Ci[j]:
                        lastcheck=0
        elif Si[i] == 2:
            sum +=Ci[i]*10
            for j in range(i+1,M):
                if Si[i] == Si[j]:
                    sum-=int(Ci[i]*10)
                    if Ci[i] != Ci[j]:
                        lastcheck=0
        else:
            sum +=Ci[i]
            for j in range(i+1,M):
                if Si[i] == Si[j]:
                    sum-=int(Ci[i])
                    if Ci[i] != Ci[j]:
                        lastcheck=0
for i in range(M):
    if Si[i] == 1:
        precheck+=1
if precheck == 0:
    if N == 3:
        sum +=100
    elif N == 2:
        sum +=10
    else:
        sum+=0
if lastcheck == 0:
    print('-1')
else:
    print(sum)