N, M = map(int, input().split())
s = [list(map(int, input().split())) for i in range(M)]

a = 0
b = 0
c = 0

for i in range(M):
    if s[i][0] == 1:
        a = 9
    elif s[i][0] == 2:
        b = 9
    else:
        c = 9
        
for i in range(M):
    if s[i][0] == 1:
        a = min(a, s[i][1])
    elif s[i][0] == 2:
        b = min(b, s[i][1])
    else:
        c = min(c, s[i][1])

a_count = 0 #予測できない場合の場合分けの準備
b_count = 0
for i in range(M):
    if N == 3 and s[i][0] == 1 and s[i][1] != 0:
        a_count = 1
    elif N == 2 and s[i][0] == 2 and s[i][1] != 0:
        b_count = 1

if N == 3 and a_count == 0 or N == 2 and b_count == 0:
    print(-1)
elif N == 3:
    print(a, b, c, sep='')
elif N == 2:
    print(b, c, sep='')
elif N == 1:
    print(c)