N,M = map(int, input().split())
ans = 0
c1 = []
c2 = []
c3 = []
for _ in range(M):
  s, c = map(int,input().split())
  if s == 1 and c == 0:
    print(-1)
    exit(0)
  elif N == 1:
    if not c in c1:
      ans += c
      c1.append(c)
      if len(c1) > 1:
        print(-1)
        exit(0)
  elif N == 2:
    if s == 1 and not c in c2:
      ans += 10*c
      c2.append(c)
      if len(c2) > 1:
        print(-1)
        exit(0)
    elif s == 2 and not c in c3:
      ans += c
      c3.append(c)
      if len(c3) > 1:
        print(-1)
        exit(0)
  elif N == 3:
    if s == 1 and not c in c1:
      ans += 100*c
      c1.append(c)
      if len(c1) > 1:
        print(-1)
        exit(0)
    elif s == 2 and not c in c2:
      ans += 10*c
      c2.append(c)
      if len(c2) > 1:
        print(-1)
        exit(0)
    elif s == 3 and not c in c3:
      ans += c
      c3.append(c)
      if len(c3) > 1:
        print(-1)
        exit(0)
print(ans)
