a = input().split(" ")
N = int(a[0])
M = int(a[1])
ans1 = 10
ans2 = 10
ans3 = 10
for i in range(M):
    b = input().split(" ")
    c = int(b[0])
    d = int(b[1])
    if c == 1:
        if d < ans1:
            ans1 = d
    elif c == 2:
        if d < ans2:
            ans2 = d
    else:
        if d < ans3:
            ans3 = d
if ans1 == 10:
    ans1 = 0
if ans2 == 10:
    ans2 = 0
if ans3 == 10:
    ans3 = 0
if N == 1:
    print(ans3)
elif N == 2:
    if ans2 == 0:
        print(-1)
    else:
        print(str(ans2) + str(ans3))
else:
    if ans1 == 0:
        print(-1)
    else:
        print(str(ans1) + str(ans2) + str(ans3))