N,M = map(int,input().split())
sc = [list(map(int,input().split())) for _ in range(M)]
three = 1000
two = 100
one = 10
for i in range(M):
  if sc[i][0]== 1:
    three = min(100*sc[i][1],three)
  if sc[i][0]== 2:
    two = min(10*sc[i][1],two)
  if sc[i][0]== 3:
    one = min(sc[i][1],one)
if three == 1000 or three == 0:
  print(-1)
  exit()
if two == 100:
  two = 0
if one == 10:
  one = 0
print(three+two+one)