n,m=map(int,input().split())

if n==3:
    mod=100
elif n==2:
    mod=10
else:
    mod=1

a=[]
for i in range(m):
    a.append(list(map(int,input().split())))


for i in range(m-1):
    for j in range(i+1,m):
        if a[i][0]==a[j][0]:
            if a[i][1]!=a[j][1]:
                print(-1)
                exit()

a.sort()
a1=10
a2=10
a3=10
if n==3:
    for i in range(m):
        if a[i][0]==1:
            a1=min(a1,a[i][1])
        if a[i][0]==2:
            a2=min(a2,a[i][1])
        if a[i][0]==3:
            a3=min(a3,a[i][1])
    

    if (a1%10)*100+(a2%10)*10+(a3%10)==0:
        print(-1)
    else:
        print((a1%10)*100+(a2%10)*10+(a3%10))

elif n==2:
    for i in range(m):
        if a[i][0]==1:
            a1=min(a1,a[i][1])
        if a[i][0]==2:
            a2=min(a2,a[i][1])
    
    if (a1%10)*10+(a2%10)==0:
        print(-1)
    else:
        print((a1%10)*10+(a2%10)*1)

else:
    for i in range(m):
        if a[i][0]==1:
            a1=min(a1,a[i][1])
    if a1%10!=0:
        print(a1%10)
    else:
        print(-1)