n, m = map(int, input().split())
tmp = []
one = 1000
two = 11000
three = 10000
for i in range(m):
    s, c = map(int, input().split())
    tup = (s, c)
    tmp.append(tup)

s = set(tmp)
for a, b in s:
    if a == 1:
        if one > b:
            one = b*100
    elif a == 2:
        if two > b:
            two = b*10
    elif a ==3:
        if three > b:
            three = b

if one == 0 and n != m:
    print(-1)
else:
    print(one + two + three)