N, M = map(int, input().split())
d = {1:-1, 2:-1, 3:-1}
for _ in range(M):
    s, c = map(int, input().split())
    if d[s]!=-1 and d[s]!=c:
        print(-1)
        break
    d[s] = c
else:
    ans = 0

    for i in range(1, 4):
        if d[i]==-1:
            d[i] = 0

    if d[4-N] == 0:
        print("NO")
    else:
        for k, v in d.items():
            ans += (v)*(10**(3-k))
        print(ans)
