N, M = map(int, input().split())
C1 = []
C2 = []
C3 = []
count = 0
for i in range(M):
  S, C =  map(int, input().split())
  if S == 1:
    C1.append(C)
  elif S == 2:
    C2.append(C)
  else:
    C3.append(C)

if len(C1) == 0:
  C1.append(0)
if len(C2) == 0:
  C2.append(0)
if len(C3) == 0:
  C3.append(0)

if N == 1:
  count += min(C1)
  if count == 0:
    print(-1)
  else:
    print(count)
elif N == 2:
  count += 10 * min(C1) + min(C2)
  if count < 10:
    print(-1)
  else:
    print(count)
else:
  count += 100 * min(C1) + 10 * min(C2) + min(C3)
  if count < 100:
    print(-1)
  else:
    print(count)