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

ans = 0
num = {}

for i in range(m):
    s,c = input().split()
    if s in num.keys():
        if c != num[s]:
            ans = -1
    num[s] = c

for i in range(1,3+1):
    if str(i) not in num.keys():
        num[str(i)] = "0"
        
num = sorted(num.items())
if ans == 0:
    print(int("".join([num[i][1]  for i in range(3)])))
else:
    print(ans)
