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

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


if b==[]:
    b=str(0)
if c==[]:
    c==str(0)

A=sorted(a)
B=sorted(b)
C=sorted(c)
if a==["0"] or a==[]:
    print(-1)

else:
    print("".join(str(A[0])+str(B[0])+str(C[0])))
