
a,b=map(int,input().split(" "))
if a==1:
    i1=[]

    for n in range(0,b):
        x,y =map(int,input().split(" "))
        if x==1:
            i1.append(y)
    i1=list(set(i1))
    if len(i1)>2:
        print(-1)
    else:
        n1=0
        if len(i1)>0:
            for con1 in  i1:
                n1=con1
        print(n1)
elif a==2:
    i1=[]
    i10=[]

    for n in range(0,b):
        x,y =map(int,input().split(" "))
        if x==1:
            i10.append(y)
        else:
            i1.append(y)
    i10=list(set(i10))
    i1=list(set(i1))
    if len(i1)>1 or len(i10)>1:
        print(-1)
    else:
        n1=0
        n10=1
        if len(i1)>0:
            for con1 in  i1:
                n1=con1
        if len(i10)>0:
            for con1 in  i10:
                n10=con1
        if i10==0:
            print(-1)
        else:
            print(n10*10+n1)

elif a==3:
    i1=[]
    i10=[]
    i100=[]

    for n in range(0,b):
        x,y =map(int,input().split(" "))
        if x==1:
            i100.append(y)
        elif x==2:
            i10.append(y)
        else:

            i1.append(y)
    i100=list(set(i100))
    i10=list(set(i10))
    i1=list(set(i1))
    #print(i100)
    #print(i10)
    #print(i1)
    if len(i1)>1 or len(i10)>1 or len(i100)>1:
        print(-1)
    else:
        n1=0
        n10=0
        n100=1
        if len(i1)>0:
            for con1 in  i1:
                n1=con1
        if len(i10)>0:
            for con1 in  i10:
                n10=con1
        if len(i100)>0:
            for con1 in  i100:
                n100=con1
        if n100==0:
            print(-1)
        else:
            print(n100*100+n10*10+n1)
