N, M = list(map(int, input().split()))

num = [0, 0, 0]
num_1 = []
num_2 = []
num_3 = []

for i in range(M):
    idx, c = list(map(int, input().split()))

    if idx == 1:
        num_1.append(c)
    if idx == 2:
        num_2.append(c)
    if idx == 3:
        num_3.append(c)

if N == 1:
    if len(num_1) == 0:
        print(0)
    else:
        print(min(num_1))

if N == 2:
    if len(num_2) == 0:
        print(-1)
    else:
        if len(num_1) == 0:
            print(min(num_2)+0)
        else:
            print(min(num_2) + min(num_1))

if N == 3:
    if len(num_3) == 0:
        print(-1)
    else:
        if len(num_1) == 0 and len(num_2) == 0:
            print(str(min(num_3)) + str(00))
        elif len(num_1) == 0 and len(num_2) > 0:
            print(str(min(num_3)) + str(0) + str(min(num_1)))
        else:
            print(str(min(num_3)) + str(min(num_2)) + str(0))