n,m = map(int,input().split())
s_c = [list(map(int,input().split())) for i in range(m)]
#print(s_c)
ans_100 = []
ans_10 = []
ans_1 = []
ans_0 = []
ans = 0
for i in range(m):

    if s_c[i][0]==1:
        ans_100.append(100*s_c[i][1])
    if s_c[i][0]==2:
        ans_10.append(10*s_c[i][1])
    if s_c[i][0]==3:
        ans_1.append(s_c[i][1])
'''
if len(set(ans_100))<2:
    ans_0.append(ans_100)
else:
    ans = -1
if len(set(ans_10))<2:
    ans_0.append(ans_10)
else:
    ans = -1
if len(set(ans_1))<2:
    ans_0.append(ans_1)
else:
    ans = -1
if len(set(ans_100))==0 and len(set(ans_10))==0 and len(set(ans_1))==1:
    ans = ans_1
#print(ans)
if ans!=-1 or ans==0:
    for i in ans_0:
        if 0<len(i) and len(i)<2:
            ans += i[0]
        elif len(i)==0:
            ans += 0
        else:
            ans += i[0]
    if ans==0:
        print(-1)
    else:
        print(ans)
else:
    print(ans)
'''
if len(set(ans_100))<2 and len(set(ans_10))<2 and len(set(ans_1))<2:
    ans_0.append(ans_100)
    ans_0.append(ans_10)
    ans_0.append(ans_1)
    if len(set(ans_100))==0 and len(set(ans_10))==0 and len(set(ans_1))==1:
        ans = ans_1
    #print(ans)
    if ans!=-1 or ans==0:
        for i in ans_0:
            if 0<len(i) and len(i)<2:
                ans += i[0]
            elif len(i)==0:
                ans += 0
            else:
                ans += i[0]
        if ans==0:
            print(-1)
        else:
            print(ans)
else:
    print(-1)