import sys

n,m = map(int,input().split())

mark1  = -1
mark2  = -1
mark3  = -1
b = 0
for i in range(m):
  s,c = map(int,input().split())
  if s == 1:
    if (mark1 != -1) and (mark1 != c):
      print(-1)
      sys.exit()
    if (mark1 != c):
      b += c
      mark1 = c
  elif s == 2:
    if (mark2 != -1) and (mark2 != c):
      print(-1)
      sys.exit()
    if (mark2 != c):
      b += c * 10
      mark2 = c
  else:
    if (mark3 != -1) and (mark3 != c):
      print(-1)
      sys.exit()
    if (mark3 != c):
      b += c * 100
      mark3 = c
if b != 0:
  print(b)
else:
  print(-1)

