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

if N == 3:
    for i in range(M):
        if P[i][0] == '1':
            if k3 != 0:
                k3 = min(k3, int(P[i][1]))
            else:
                k3 = int(P[i][1])
        elif P[i][0] == '2':
            if k2 != 0:
                k2 = min(k2, int(P[i][1]))
            else:
                k2 = int(P[i][1])
        else:
            if k1 != 0:
                k1 = min(k1, int(P[i][1]))
            else:
                k1 = int(P[i][1])
    ans = k3 * 100 + k2 * 10 + k1
    ans = ans if M > 0 and len(str(ans)) == 3  else - 1
    print(ans)

elif N == 2:
    for i in range(M):
        if P[i][0] == '1':
            print(-1)
            exit(1)
        elif P[i][0] == '2':
            if k2 != 0:
                k2 = min(k2, int(P[i][1]))
            else:
                k2 = int(P[i][1])
        else:
            if k1 != 0:
                k1 = min(k1, int(P[i][1]))
            else:
                k1 = int(P[i][1])

    ans = k3 * 100 + k2 * 10 + k1
    ans = ans if M > 0 and len(str(ans)) == 2  else - 1
    print(ans)
        
        
else:
    for i in range(M):
        if P[i][0] == '1' or P[i][0] == '2':
            print(-1)
            exit(1)
        else:
            if k1 != 0:
                k1 = min(k1, int(P[i][1]))
            else:
                k1 = int(P[i][1])

    ans = k3 * 100 + k2 * 10 + k1
    ans = ans if M > 0 and len(str(ans)) == 1  else - 1
    print(ans)