import sys


def get():
    return list(map(int, input().split()))


# 未完成
a = get()
if a[0] < a[1] or a[0] == 0:
    print(-1)
    sys.exit(0)
b = []
for i in range(a[1]):
    array = get()
    b.append(array)
count = 0
flag = False

while (not flag and count < 1000):
    keta = 1
    ans = [int(count / 100) % 10, int(count / 10) % 10, count % 10]
    if sum(ans) != 0 and ans[0] == 0 and ans[1] != 0:
        keta = 2
    elif sum(ans) != 0 and ans[0] != 0:
        keta = 3
    if a[0] != keta:
        count += 1
        continue
    if (a[1] == 0):
        flag = True
    for i in range(len(b)):
        if ans[b[i][0]-1] == b[i][1]:
            flag = True
        else:
            flag = False
            break
    count += 1

if flag:
    print(count - 1)
else:
    print(-1)