import sys
n,m = map(int,input().split())
S = []
C = []
hand = 0
ten = 0
one = 0
anshand= 0
ansten = 0
ansone = 0
for i in range(m):
  s,c = map(int,input().split())
  S.append(s)
  C.append(c)
  
for i in range(m):
  if S[i] == 3:
    if one == 0:
      one += 1
      ansone += C[i]
    elif one == 1:
      if C[i] != ansone:
        print("-1")
        sys.exit()
        
  if S[i] == 2:
    if ten == 0:
      ten += 1
      ansten += C[i]*10
    elif ten == 1:
      if C[i]*10 != ansten:
        print("-1")
        sys.exit()
        
  if S[i] == 1:
    if hand == 0:
      if C[i] == 0:
        print("-1")
        sys.exit()
      else:
        hand += 1
        anshand += C[i] *100
    elif hand == 1:
      if C[i]*100 != anshand:
        print("-1")
        sys.exit() 
        
print(ansone+ansten+anshand)