N, M = map(int, input().split())
P = [input().split() for i in range(M)]

k3 = 10
k2 = 10
k1 = 10


for i in range(M):
    if P[i][0] == '3':
        k1 = min(k1, int(P[i][1]))
    elif P[i][0] == '2':
        k2 = min(k2, int(P[i][1]))
    else:
        k3 = min(k3, int(P[i][1]))

ans = k3 * 100 + k2 * 10 + k1 if M > 0 else -1

if N == 3:
    if len(str(ans)) == 3:
        print(ans)
    else:
        print(-1)
elif N == 2:
    if len(str(ans)) == 2:
        print(ans)
    else:
        print(-1)
else:
    if len(str(ans)) == 1:
        print(ans)
    else:
        print(-1)
    