N,M=map(int,input().split())
S_C=[]
for _ in range(M):
    S_C.append(list(map(int,input().split())))

tmp=[]
for S,C in S_C:
    tmp.append(10**(3-S)*C)

a=[]
tmp=set(tmp)
for i in tmp:
    if len(str(i)) in a:
        print(-1)
        exit()
    else:
        a.append(len(str(i)))

print(sum(set(tmp)) if len(str(sum(tmp))) == N else -1)
