N, M = map(int, input().split())
SC = [list(map(int, input().split())) for i in range(M)]
a = 0
for i in range(M):
    if SC[i][0] != N or (SC[i][0] == 1 and SC[i][1]==0):
        a += 1

handred = 0
ten = 0
one = 0

for i in range(M):
    if SC[i][0] == 1:
        handred = 9
    elif SC[i][0] == 2:
        ten = 9
    elif SC[i][0] == 3:
        one = 9

if a == M:
    print('-1')
else:
    for i in range(M):
        if SC[i][0] == 1:
            if handred >= SC[i][1]:
                handred = SC[i][1]
        elif SC[i][0] == 2:
            if ten >= SC[i][1]:
                ten = SC[i][1]
        elif SC[i][0] == 3:
            if one >= SC[i][1]:
                one = SC[i][1]

if a != M and N == 3:
    print(str(handred) + str(ten) + str(one))
elif a != M and N == 2:
    print(str(ten) + str(one))
elif a != M and N==1:
    print(str(one))