N, M=map(int,input().split())
s=list()
c=list()
a=0
for i in range(0,M):
    s.append(0)
    c.append(0)
for i in range(0,M):
    s[i], c[i]=map(int,input().split())
for i in range(0,M):
    for j in range(0,M):
        if s[i]==s[j] and c[i]!=c[j]:
            print("-1")
            break
    else:
        continue
    break
else:
    for i in range(0,M):
        a*=s[i]
    if a==1:
        b=s[0]
        print(b)
    elif a%3==0:
        b=[0,0,0]
        for i in range(0,M):
            b[s[i]-1]=c[i]
        print("{}{}{}".format(b[0],b[1],b[2]))
    else:
        b=[0,0]
        for i in range(0,M):
            b[s[i]-1]=c[i]
        print("{}{}".format(b[0],b[1]))