N,M=map(int,input().split())
s=[]
c=[]
ans=0
for i in range(M):
    x,y=map(int,input().split())
    s.append(x)
    c.append(y)
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]!=c[j]:
            print(-1)
            exit()
for i in range(M):
    if N<s[i]:
        print(-1)
        exit()
for i in range(M):
    if s[i]==1 and c[i]==0 and N>1:
        print(-1)
        exit()
for i in range(M-1):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]==c[j]:
            s[j]=0
            c[j]=0
if N==1:
    print(c[0])
if N==2:
    for i in range(M):
        if s[i]==1:
            ans+=10*c[i]
        if s[i]==2:
            ans+=c[i]
    print(ans)
    exit()
if N==3:
    for i in range(M):
        if s[i]==1:
            ans+=100*c[i]
        if s[i]==2:
            ans+=10*c[i]
        if s[i]==3:
            ans+=c[i]
    print(ans)