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

k1 = [100, 10, 1]
an =[]
zeros =[]
for i in range(m):
  if c[i] == 0:
    zeros.append(s[i])
  an.append(c[i] * k1[s[i] - 1])

for i in range(m):
  for j in range(m):
    if s[i] == s[j]:
      if c[i] != c[j]:
        print("-1")
        exit()
        
if 10 ** max(zeros) > sum(an):
  print("-1")
  exit()
for i in range(m):
  if an.count(an[i]) != 1:
    an[i] = 0
if sum(an) == 0:
  print("-1")
  exit()
print(sum(an))