N, M = map(int, input().split())
S = [0]*M
C = [0]*M
for i in range(M):
    S[i], C[i] = map(int, input().split())

sa = 0
Flg = True

for j in range(M):
    for k in range(j, M):
        if(S[j] == S[k]):
            if(C[j] != C[k]):
                Flg = False
S = list(set(S))

if(Flg == True and N in S):
    for i, value in enumerate(S):
        sa += (10**(3-value))*C[i]
    print(sa)
else:
    print(-1)  