l = input().split()
N = int(l[0])
M = int(l[1])
sc = [input().split() for x in range(M)]
SC = [[int(x) for x in X] for X in sc]

sorted_SC = []
sorted_SC =[ x for x in SC if x not in sorted_SC and not sorted_SC.append(x)]

flag = True
s = [x[0] for x in sorted_SC]
for i in range(3):
    if s.count(i) > 1:
        print('-1')
        flag = False

a = 0
b = 0
c = 0

if flag:
    for l in sorted_SC:
        if l[0] == 1:
            c = l[1]
        if l[0] == 2:
            b = l[1]
        if l[0] == 3:
            a = l[1]
    ans = int(a*100 + b*10 + c)
    print(ans)