from itertools import product
n,m = map(int,input().split())
lists = [list(map(str,input().split())) for i in range(m)]
keta_1 = []
keta_2 = []
keta_3 = []
li = []
for keta,atai in lists:
    if keta == "1":
        keta_1.append(atai)
    elif keta == "2":
        keta_2.append(atai)
    else:
        keta_3.append(atai)
if len(keta_1) == 0:
    keta_1.append("0")
if len(keta_2) == 0:
    keta_2.append("0")
if len(keta_3) == 0:
    keta_3.append("0")
for i,j,k in product(keta_1,keta_2,keta_3):
    tmp = [i,j,k]
    tmp = int("".join(tmp))
    if len(str(tmp)) == n:
        li.append(int(tmp))
if len(li) == 0:
    print(-1)
else:
    print(min(li))