n,m = map(int,input().split())
lis1 = []
lis2 = []
lis3 = []
for i in range(m):
    s,c = map(int,input().split())
    if s == 1:
        lis1.append(c)
    elif s == 2:
        lis2.append(c)
    else:
        lis3.append(c)

lis1.sort()
lis2.sort()
lis3.sort()

t = 0

for i in range(len(lis1)-1):
    if lis1[i] != lis1[i+1] :
        print(-1)
        break
    else:
        t += 1
for i in range(len(lis2)-1):
    if lis2[i] != lis2[i+1] :
        print(-1)
        break
    else:
        t += 1
for i in range(len(lis3)-1):
    if lis3[i] != lis3[i+1] :
        print(-1)
        break
    else:
        t += 1

if t ==m-2 :
    if len(lis1) != 0:
        if lis1[0] != 0:
            ans = str(lis1[0])
    if len(lis2)!=0:
        if lis2[0] != 0:
            ans += str(lis2[0])
        else:
            if len(lis1) != 0:
                if lis1[0] != 0:
                    ans += "0"
    else:
        if len(lis1) != 0:
            if lis1[0] != 0:
                ans += "0"
    if len(lis3)!=0:
        if lis3[0] != 0:
            ans += str(lis3[0])
        else:
            if len(lis2) != 0:
                if lis2[0] != 0:
                    ans += "0"
    else:
        if len(lis2) != 0:
            if lis2[0] != 0:
                ans += "0"
print(ans)
