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 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
    
    
    
for i in range(0,M):
  for j in range(i,M):
    if s[i] == s[j] and c[i] != c[j]:
      ans = -1

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