N,M = map(int, input().split())
L = []
K = []


A = []
B = []
C = []
D = []
E = []
F = []
G = []
H = []

for i in range(M):
    s,c = map(int, input().split())
    L.append([s,c])
for i in range(M):
    for j in range(M):
        if L[i][0] == L[j][0] and L[i][1] != L[j][1]:
            K.append(-1)
            break
    break
if len(K) == 1:
    print(K[0])

elif N == 1:
    if M == 0:
        print(0)
    else:
        for a in range(M):
            A.append(L[a][1])
        print(min(A))
elif N == 2:
    for b in range(M):
        if L[b][0] == 1 and L[b][1] != 0:
            B.append(L[b][1])
        elif L[b][0] == 2:
            C.append(L[b][1])
        elif L[b][0] == 1 and L[b][1] == 0:
            G.append(-1)
    if len(set(G)) == 1:
        print(-1)
    elif B == [] and C == []:
        print(0)
    elif B == []:
        print(min(C))
    elif C == []:
        print(str(min(B)) + '0')
    else:
        x = min(B)
        y = min(C)
        print(str(x) + str(y))

elif N == 3:
    for b in range(M):
        if L[b][0] == 1 and L[b][1] != 0:
            D.append(L[b][1])
        elif L[b][0] == 2:
            E.append(L[b][1])
        elif L[b][0] == 3:
            F.append(L[b][1])
        elif L[b][0] == 1 and L[b][1] == 0:
            H.append(-1)
    if len(set(H)) == 1:
        print(-1)
    elif D == [] and E == [] and F == []:
        print(0)
    elif D == [] and E == []:
        print(min(F))
    elif D == [] and F == []:
        print(str(min(E)) + '0')
    elif E == [] and F == []:
        print(str(min(D)) + '00')
    elif E == []:
        print(str(min(D)) + '0' + str(min(F)))
    elif F == []:
        print(str(min(D)) + str(min(E)) + '0')
    else:
        print(str(min(D)) + str(min(E)) + str(min(F)))