n, m = map(int, input().split())
tmp = []
one = 0
two = 0
three = 0
for i in range(m):
    s, c = map(int, input().split())
    tup = (s, c)
    tmp.append(tup)

s = set(tmp)
for a, b in s:
    if a == 1:
        if one < b:
            one = b
    elif a == 2:
        if two < b:
            two = b
    elif a ==3:
        if three < b:
            three = b

if one == 0 and n != m:
    print(-1)
else:
    print("{}{}{}".format(one, two, three))