n,m=map(int, input().split())

sc = []
for i in range(m):
    sc.append(list(map(int, input().split())))

def has_duplicates(seq):
    return len(seq) != len(set(seq))
s_list = []
c_list = []


for i,(s,c) in enumerate(sc):
    s_list.append(s)
    c_list.append(c)

anc = []
if 1 in s_list:
    anc.append(str(c_list[s_list.index(1)]))
else:
    anc.append('0')
if 2 in s_list:
    anc.append(str(c_list[s_list.index(2)]))
else:
    anc.append('0')
if 3 in s_list:
    anc.append(str(c_list[s_list.index(3)]))
else:
    anc.append('0')

anc_c = [c_list[i] for i,x in enumerate(s_list) if s_list.count(x) > 1]

if has_duplicates(s_list) and not has_duplicates(anc_c):
    print(-1)
elif has_duplicates(s_list) and has_duplicates(anc_c):
    if int(''.join(anc)) == 0:
        print(-1)
    else: 
        print(int(''.join(anc)))
        
elif not has_duplicates(s_list):
    if int(''.join(anc)) == 0:
        print(-1)
    else: 
        print(int(''.join(anc)))
