import sys

N, M = map(int, input().split())

x = []
y = []
z = []

for i in range(M):
    s1, c1 = map(int, input().split())
    if s1 == 1:
        x.append(c1)
    elif s1 == 2:
        y.append(c1)
    elif s1 == 3:
        z.append(c1)

if N == 1:
    if len(x) == 0:
        print('-1')
        sys.exit()

    x.sort()
    print(str(x[0]))
    sys.exit()

elif N == 2:
    if len(x) == 0:
        print('-1')
        sys.exit()

    if len(y) == 0:
        y.append(0)

    x.sort()
    y.sort()

    if x[0] == 0:
        print('-1')
        sys.exit()

    print(str(x[0]) + str(y[0]))
    sys.exit()

elif N == 3:
    if len(x) == 0:
        print('-1')
        sys.exit()

    if len(y) == 0:
        y.append(0)
    if len(z) == 0:
        z.append(0)

    x.sort()
    y.sort()
    z.sort()

    if x[0] == 0:
        print('-1')
        sys.exit()

    print(str(x[0]) + str(y[0]) + str(z[0]))
    sys.exit()

print('-1')
