n , m = map(int,input().split())
s = [0] * m
c = [0] * m
for i in range(m):
    s[i],c[i] = map(int,input().split())

num = [0] * 4
ans = 0

for i in range(m):
    if num[s[i]] == 0:
        num[s[i]] = c[i]
    elif num[s[i]] == c[i]:
        num[s[i]] = c[i]
    else:
        ans = -1
        break
s = ""
for i in range(1,4):
    s += str(num[i])
if ans == -1:
    print(ans)
elif int(s) == 0:
    print("-1")
else:
    print(int(s))