
# N桁
N, M = map(int, input().split())

from collections import defaultdict

l = defaultdict(lambda: 10**9)

for m in range(M):
    s,c = map(int, input().split())
    if s==1 and c==0:# 先頭桁を0にしない
        continue
    else:
        l[s] = min(l[s], c*10**(3-s)) # max3桁の場合、左から3番目は配列の0
        #print(c*10**(3-s))

#print("---")
ans = 0
for n in range(3+1):
    if l[n]!=10**9:
        ans += l[n]
        #print(l[n])
    
#print(ans, len(str(ans)))
if len(str(ans)) == N:
    print(ans)
else:
    print(-1)