
N,M =map(int, input().split())
s = []
c = []
for i in range(M):
    x1,y1=[int(i) for i in input().split()]
    s.append(x1)
    c.append(y1)

ans = 0
check1 = 0
check2 = 0
check3 = 0

for i in range(0,M):
    if N == 3:
        if s[i] == 3 and check1 == 0:
            ans += c[i]
            check1 = 1
        elif s[i] == 2 and check2 == 0:
            ans += c[i]*10
            check2 = 1
        elif s[i] == 1 and check3 == 0:
            ans += c[i]*100
            check3 = 1
    elif N == 2:
        if s[i] == 2 and check2 == 0:
            ans += c[i]*1
            check2 = 1
        elif s[i] == 1 and check3 == 0:
            ans += c[i]*10
            check3 = 1
    elif N == 1:
        if s[i] == 1 and check2 == 0:
            ans += c[i]*1
            check2 = 1
    
    
for i in range(0,M):
  for j in range(i,M):
    if s[i] == s[j] and c[i] != c[j]:
      ans = -1
if N == 3:
    for i in range(0,M):  
        if s[i] == 1 and c[i] == 0:
            ans = -1
if N == 2:
    for i in range(0,M):  
        if s[i] == 1 and c[i] == 0:
            ans = -1
        
print(ans)

      
