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())

dic = dict(zip(s, c))
result = [0, 0, 0]
for i in range(3):
    if len(set([c[s == i+1]]))>=2:
        result = -1
        types = 'int'
    else:
        types = 'str'
        if i+1 in dic.keys():
            result[i] = dic[i+1]
        else:
            pass

if 1 in s:
    if c[s==i]==0:
        result = -1
        types = 'int'
          
results = ''       
if types=='types':
    for i in result:
        results += str(i)
    print(int(results))
else:
    print(result)