N, M = map(int, input().split())
s = [list(map(int, input().split())) for i in range(M)]
possible = True
true_s = []

for i in range(1, 4): # 各桁
    clis = []
    for j in range(M):
        if s[j][0] == i:
            digit_num = s[j][1]
            clis.append(digit_num)
    if len(clis) > 0:
        if all([c == digit_num for c in clis]):
            true_s.append([i, digit_num])
        else:
            possible = False
            
if possible:
    num = []
    for i in range(1, 4):
        for j in range(len(true_s)):
            if i == true_s[j][0]:
                num.append(true_s[j][1])
                break
        else:
            num.append('0')
    print(str(int(''.join(num))))
else:
    print('-1')