n,m=map(int,input().split())

if n==1:
    for i in range(m):
        s,c=map(int,input().split())
        if s==1:
            print(c)
        else:
            print("-1")

if n==2:
    ans=0
    cnt10=0
    cnt1=0
    tmp10=0
    tmp1=0
    for i in range(m):
        s,c=map(int,input().split())
        if s==1 and c==0:
            ans=-1
            break
        if s>2:
            ans=-1
            break
        elif s==1 and cnt10<1:
            tmp10=c
            ans+=tmp10*10
            cnt10+=1
        elif s==1 and cnt10>=1:
            if c != tmp10:
                ans=-1
                break
            else:
                cnt10+=1
        elif s==2 and cnt1<1:
            tmp1=c
            ans+=tmp1
            cnt1+=1
        elif s==2 and cnt1>=1:
            if c != tmp1:
                ans=-1
                break
            else:
                cnt1+=1    
    print(ans)   
    

if n==3:
    ans=0
    cnt100=0
    cnt10=0
    cnt1=0
    tmp100=0
    tmp10=0
    tmp1=0
    for i in range(m):
        s,c=map(int,input().split())
        if s==1 and c==0:
            ans=-1
            break
        if s>3:
            ans=-1
            break
        elif s==1 and cnt100<1:
            tmp100=c
            ans+=tmp100*100
            cnt100+=1
        elif s==1 and cnt100>=1:
            if c != tmp100:
                ans=-1
                break
            else:
                cnt100+=1
        elif s==2 and cnt10<1:
            tmp10=c
            ans+=tmp10*10
            cnt10+=1
        elif s==2 and cnt10>=1:
            if c != tmp10:
                ans=-1
                break
            else:
                cnt10+=1    
        elif s==3 and cnt1<1:
            tmp1=c
            ans+=tmp1
            cnt1+=1
        elif s==3 and cnt1>=1:
            if c != tmp1:
                ans=-1
                break
            else:
                cnt1+=1
    print(ans)      
