N,M=map(int,input().split())
sc=[list(map(int,input().split())) for i in range(M)]
s=[sc[i][0] for i in range(M)]
c=[sc[i][1] for i in range(M)]
sc.sort()
sets=list(set(s))
setc=list(set(c))
if len(sets)!=len(setc):
    print(-1)
if sets==0 and setc==0:
    print(0)
if sc[0][0]==1 and sc[0][1]==0:
    print(-1)
else:
    if len(sets)==3:
        print(sc[0][1]*100+sc[1][1]*10+sc[2][1])
    elif len(sets)==2:
        if len(sc)==3:
            sc=[sc[0],sc[2]]
        if sc[0][0]==1:
            if sc[1][0]==2:
                print(sc[0][1]*100+sc[1][1]*10)
            if sc[1][0]==3:
                print(sc[0][1]*100+sc[1][1])
        if sc[0][0]==2:
            print(100+sc[0][0]*10+sc[0][1])
    elif len(sets)==1:
        if sc[0][0]==1:
            print(sc[0][1]*100)
        if sc[0][0]==2:
            print(100+sc[0][1]*10)
        if sc[0][0]==3:
            print(100+sc[0][1])