n, m = map(int, input().split())

jouken_1 = []
jouken_2 = []
jouken_3 = []

for _ in range(m):
    array = tuple(map(int, input().split()))
    if array[0] == 1:
        jouken_1.append(array)
    elif array[0] == 2:
        jouken_2.append(array)
    elif array[0] == 3:
        jouken_3.append(array)

a = set(jouken_1)
b = set(jouken_2)
c = set(jouken_3)
a = list(a)
b = list(b)
c = list(c)

if len(a) > 1:
    print(-1)
    exit()
elif len(b) > 1:
    print(-1)
    exit()
elif len(c) > 1:
    print(-1)
    exit()

if n == 1:
    if len(b) > 0 or len(c) > 0:
        print(-1)
        exit()
    else:
        print(a[0][1])
elif n == 2:
    if len(c) > 0:
        print(-1)
        exit()
    elif a[0][1] == 0:
        print(-1)
        exit()
    elif len(b) == 0 and len(a) == 1:
        num = a[0][1] * 10
        print(num)
    elif len(a) == 1 and len(b) == 1:
        num = b[0][1]+ a[0][1] * 10
        print(num)
elif n == 3:
    if a[0][1] == 0:
        print(-1)
        exit()
    if len(b) == 1:
        if len(c) == 1:
            num = a[0][1] * 100 + b[0][1] * 10 + c[0][1]
        else:
            num = a[0][1] * 100 + b[0][1] * 10
    else:
        if len(c) == 1:
            num = a[0][1] * 100 + c[0][1]
        else:
            num = a[0][1] * 100
    print(num)
