n,m = [int(x) for x in input().split()]
li = list()
for i in range(m):
    li.append([int(x) for x in input().split()])

num = list()
flag1 = False
flag2 = False
flag3 = False
for i in li:
    if (i[0] > n):
        print(-1)
        break
    if (i[0] == 1):
        if (n == 3 and i[1] == 0):
            print(-1)
            break
        if flag1 :
            if (not i[1]*100 in num ):
                print(-1)
                break
        if not flag1:
            num.append(i[1]*100)
        flag1 = True
    if (i[0] == 2):
        if (n == 2 and i[1] == 0):
            print(-1)
            break
        if flag2:
            if (not i[1]*10 in num):
                print(-1)
                break
        if not flag2:
            num.append(i[1]*10)
        flag2 = True
    if (i[0] == 3):
        if flag3:
            if (not i[1] in num):
                print(-1)
                break
        if not flag3:
            num.append(i[1])
        flag3 = True
else:
    if (n == 3 and not flag1):
        print(100+sum(num))
    elif (n==2 and not flag2):
        print(sum(num)+10)
    else:
        print(sum(num))