N,M=map(int,input().split())
s=[0]*M
c=[0]*M
for i in range(M):
    s[i],c[i]=map(int,input().split())
    
if N==1 and M!=0:
    flag_1=0
    for i in range(M-1):
        for j in range(i+1,M):
            if c[i]!=c[j]:
                flag_1+=1
    if flag_1==0:
        print(c[0])
    else:
        print(-1)
elif N==1 and M==0:
    print(0)

elif N==2:
    flag_2_ok=0
    for i in range(M):
        if s[i]==1 and c[i]!=0:
            flag_2_ok+=1
    if flag_2_ok>=1:
        ans_2=10
        flag_2=0
        flag_2_ten=0
        flag_2_one=0
        for i in range(M-1):
            for j in range(i+1,M):
                if s[i]==s[j]:
                    if c[i]!=c[j]:
                        flag_2+=1
        if flag_2==0:
            for i in range(M):
                if s[i]==1 and c[i]!=0:
                    if s[i]==1 and flag_2_ten==0:
                        ans_2+=10*c[i]-10
                        flag_2_ten+=1
                    if s[i]==2 and flag_2_one==0:
                        ans_2+=c[i]
                        flag_2_one+=1
            print(ans_2)
        else:
            print(-1)
    else:
        print(-1)
elif N==2 and M==0:
    print(0)
        
elif N==3:
    flag_3_ok=0
    for i in range(M):
        if s[i]==1 and c[i]!=0:
            flag_3_ok+=1
    if flag_3_ok>=1:
        ans_3=100
        flag_3=0
        flag_3_hun=0
        flag_3_ten=0
        flag_3_one=0
        for i in range(M-1):
            for j in range(i+1,M):
                if s[i]==s[j]:
                    if c[i]!=c[j]:
                        flag_3+=1
        if flag_3==0:
            for i in range(M):
                if s[i]==1 and flag_3_hun==0:
                    ans_3+=100*c[i]-100
                    flag_3_hun+=1
                if s[i]==2 and flag_3_ten==0:
                    ans_3+=10*c[i]-10
                    flag_3_ten+=1
                if s[i]==3 and flag_3_one==0:
                    ans_3+=c[i]
                    flag_3_one+=1
            print(ans_3)
        else:
            print(-1)
    else:
        print(-1)
elif N==3 and M==0:
    print(0)