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

num = [0,0,0]
cnt = [0,0,0]
for i in range(m):
    keta = sc[i][0] - 1
    if num[i] == 0:
        num[keta] = sc[i][1]
    elif num[i] != 0 and  num[i] > sc[i][1]:
        num[keta] = sc[i][1]


result = str(num[0]) + str(num[1]) + str(num[2]) 
result = int(result)

keta = (len(str(result)))
if n > keta:
    print("-1")
else:
    print(result)


