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])
#print(len(set(ans_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

for i in ans_0:
    #print(i)
    if 0<len(i) and len(i)<2:
        ans += i[0]
    elif len(i)==0:
        ans += 0
    else:
        ans += i[0]

print(ans)