n, m, *s_c = map(int, open(0).read().split())

for i in range(0, m + 1, 2):
    s_c[i] -= 1

flag = False
for i in range(0, m + 1, 2):
    if flag:
        break
    for j in range(0, m + 1, 2):
        #print(i, j)
        #print(s_c[i], s_c[j], s_c[i + 1], s_c[j + 1])
        if s_c[i] == s_c[j] and s_c[i + 1] != s_c[j + 1]:
            flag = True
            break

if flag:
    print(-1)
else:
    num = [0] * 3
    for i in range(0, m, 2):
        num[s_c[i]] = s_c[i + 1] 
    if num[0] == 0:
        print(-1)
    else:
        print(''.join([str(n) for n in num]))