import sys

N, M = map(int, input().split())
SC = list()
for i in range(M):
    s, c = map(int, input().split())
    SC.append([s, c])

SSC = sorted(SC, key=lambda x: x[0])

ansL = [list(filter(lambda x:x[0]==1, SSC)), list(filter(lambda x:x[0]==12, SSC)), list(filter(lambda x:x[0]==3, SSC)),]

for L in ansL:
    if L == []:
        continue
    else:
        for l in L:
            if L[0][1] != l[1]:
                print("-1")
                sys.eixt()

ans = ""
for l in ansL:
    if l == []:
        ans += str(0)
    else:
        ans += str(min([x[1] for x in l]))

if ans[0] == "0":
    print(-1)
    sys.exit()
print(ans)


