n, m = map(int, input().split())
sc = [[int(i) for i in input().split()] for i in range(m)]
if n == 1:
    answer = 9
    for i in range(m):
        if answer > sc[i][1]:
            answer = sc[i][1]
elif n == 2:
    ans = [10, 10]
    for i in range(m):
        if sc[i][0] == 1:
            if ans[0] > sc[i][1]:
                ans[0] = sc[i][1]
        else:
            if ans[1] > sc[i][1]:
                ans[1] = sc[i][1]
    if ans[1] == 10:
        ans[1] = 0
    if ans[0] == 10 or ans[0] == 0:
        answer = -1
    else:
        answer = ans[0] * 10 + ans[1]
else:
    ans = [10, 10, 10]
    for i in range(m):
        if sc[i][0] == 1:
            if ans[0] > sc[i][1]:
                ans[0] = sc[i][1]
        elif sc[i][0] == 2:
            if ans[1] > sc[i][1]:
                ans[1] = sc[i][1]
        else:
            if ans[2] > sc[i][1]:
                ans[2] = sc[i][1]
    if ans[1] == 10:
        ans[1] = 0
    if ans[2] == 10:
        ans[2] = 0
    if ans[0] == 10 or ans[0] == 0:
        answer = -1
    else:
        answer = ans[0] * 100 + ans[1] * 10 + ans[2]
sc = list(set(sc))
for i in range(len(sc) - 1):
    if sc[i][0] == sc[i + 1][0]:
        answer = -1
print(answer)
