N,M = map(int,input().split())
SC = [list(map(int,input().split())) for _ in range(M)]
ans = 0
c1 = 0
c2 = 0
c3 = 0
check1 = 0
check2 = 0
check3 = 0
for s,c in SC:
  if s == 3:
    if c != c3 and check3 == 1:
      print(-1)
      exit()
    else:
      if c == c3:
        pass
      else:
        ans += c
        c3 = c
        check3 = 1
  if s == 2:
    if c != c2 and check2 == 1:
      print(-1)
      exit()
    else:
      if c == c2:
        pass
      else:
        ans += c*10
        c2 = c
        check2 = 1
  if s == 1:
    if c != c1 and check1 == 1:
      print(-1)
      print(c,c1)
      exit()
    else:
      if c == c1:
        pass
      else:
        ans += c*100
        c1 = c
        check1 = 1
if ans == 0:
  print(-1)
else:
  print(ans)