N,M=map(int,input().split())
s=[input().split() for i in range(M)]
a=[]
b=[]
c=[]

for j in range(0,M):
    if s[j][0]=="1":
        a.append(s[j][1])
    
    elif s[j][0]=="2":
        b.append(s[j][1])
    
    elif s[j][0]=="3":
        c.append(s[j][1])

if a==[]:
    a.append("1")
if b==[]:
    b.append("0")
if c==[]:
    c.append("0")

A=set(a)
B=set(b)
C=set(c)

if len(A)>=2 or len(B)>=2 or len(C)>=2  :
    print(-1)

elif a[0]==str(0):
    print(-1)

else:
    print("".join(a[0]+b[0]+c[0]))