n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]

l1=[]
l2=[]
l3=[]
for i in range(m):
    if s[i][0]==1:
        l1.append(s[i])
    elif s[i][0]==2:
        l2.append(s[i])
    elif s[i][0]==3:
        l3.append(s[i])
l1=list(map(list, set(map(tuple, l1))))
l2=list(map(list, set(map(tuple, l2))))
l3=list(map(list, set(map(tuple, l3))))

check=0
if len(l1)>1 or len(l2)>1 or len(l3)>1:
    check+=1
    print(-1)
if len(l1)==0 and len(l2)==0 and len(l3)==0:
    check+=1
    print(-1)
if check==0:
    ans=[]
    if len(l1)>0:
        ans.append(l1[0][1])
    else:
        ans.append(0)
    if len(l2)>0:
        ans.append(l2[0][1])
    else:
        ans.append(0)
    if len(l3)>0:
        ans.append(l3[0][1])
    else:
        ans.append(0)
    if len(l1)==0:
        del ans[0]
        if len(l2)==0:
            del ans[0] 
    print(int("".join(map(str,ans))))