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())

ans=0

for i in range(M):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]==c[j]:
            c[j]=10


for i in range(M):
    if c[i]<10:
        ans=ans+c[i]*10**(3-s[i])


for i in range(M):
    for j in range(i+1,M):
        if s[i]==s[j] and c[i]!=c[j] and c[i]<10 and c[j]<10:
            ans=-1

for i in range(M):
    if s[i]==1 and c[i]==0 and N!=1:
        ans=-1

print(ans)
