N,M=list(map(int,input().split()))
s=[list(map(int,input().split())) for i in range(M)]

Num=0
A=[]
B=[]
C=[]
        
if N==3:
    for i in s:
        if i[0]==1:
            A.append(i[1])
            
        elif i[0]==2:
            B.append(i[1])
            
        elif i[0]==3:
            C.append(i[1])
        
    if len(A)==0:
        print(-1)
    elif min(A)==0:
        print(-1)
    else:
        Num+=min(A)*100
        if len(B)==0:
            pass
        elif len(B)!=0:
            Num+=min(B)*10
        if len(C)==0:
            pass
        elif len(C)!=0:
            Num+=min(C)
        print(Num)
        
elif N==2:
    for i in s:
        if i[0]==1:
            B.append(i[1])
            
        elif i[0]==2:
            C.append(i[1])
        
    if len(B)==0:
        print(-1)
    elif min(B)==0:
        print(-1)
    else:
        Num+=min(B)*10
        if len(C)==0:
            pass
        elif len(C)!=0:
            Num+=min(C)
        print(Num)

elif N==1:
    for i in s:
        if i[0]==1:
            C.append(i[1])
        
    if len(C)!=0:
        N+=min(C)
        print(Num)
    else:
        print(Num)
    
