N, M = map(int, input().split())
d = {1: 1, 2: 1, 3: 1}
S = []
C = []
for i in range(M):
    s, c = map(int,input().split())
    S.append(s)
    C.append(c)
    d[s] = c
if (1, 0) in d.items():
    print(-1)
    exit()
for j in range(0,M-1):
    for k in range(j+1, M):
        if S[j] == S[k] and C[j] != C[k]:
            print(-1)
            exit()
print(d[1]*100+d[2]*10+d[3])