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:
        one = b
    elif a == 2:
        two = b
    elif a ==3:
        three = b

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