nm=input().rstrip().split(" ")
n =int(nm[0])
m= int(nm[1])
lines=[]
import sys
for line in sys.stdin.readlines():
    nums=line.rstrip().split()
    lines.append(nums)
#print(lines)
linga=[]
lingb=[]

linesd =list(map(list, set(map(tuple, lines))))
print(linesd)
for i in range(len(linesd)):
    linga.append(int(linesd[i][0]))
    lingb.append(int(linesd[i][1]))
print(linga)
print(lingb)
i1=0
i2=0
i3=0
for j in linga:
    if j ==1:
        i1 =i1+1
    elif j==2:
        i2 +=1
    elif j==3:
        i3 +=1
if i1>=2 or i2>=2 or i3>=2:
    print("-1")
elif n==1:
    if len(lingb)==0:
        print("0")
    else:
        print(lingb[0])
elif n==2:
    if len(lingb)==0:
        print("10")
    elif len(lingb)==1:
        if linga[0]==1:
            if lingb[0]==0:
                print("-1")
            else:
                print(10*lingb[0])
        elif linga[0]==2:
            print(10+lingb[0])
    elif len(lingb)==2:
        if linga[0]==1:
            if lingb[0]==0:
                print("-1")
            else:
                print(10*lingb[0]+lingb[1])
        if linga[0]==2:
            if lingb[1]==0:
                print("-1")
            else:
                print(10*lingb[1]+lingb[0])
elif n==3:
    if len(lingb)==0:
        print("-1")
    elif len(lingb)==1:
        if linga[0]==1:
            if lingb[0]==0:
                print("-1")
        elif linga[0]==2:
            print(100+10*lingb[0])
        elif linga[0]==3:
            print(100+lingb[0])
    elif len(lingb)==2:
        if linga[0]==1:
            if lingb[0]==0:
                print("-1")
            elif linga[1]==2:
                print(100*lingb[0]+10*lingb[1])
            elif linga[1]==3:
                print(100*lingb[0]+lingb[1])
        elif linga[0]==2:
            if linga[1]==1:
                if lingb[1]==0:
                    print("-1")
                else:
                    print(100*lingb[1]+10*lingb[0])
            if linga[1]==3:
                print(100+10*lingb[0]+lingb[1])
        #elif linga[]