import numpy as np
n,m = map(int,input().split())
c = [[int(j) for j in input().split()] for i in range(m)]
c = sorted(c)
c = np.array(c)
if n == 1 and m == 0:
    print(0)
    exit()
elif n == 2 and m == 0:
    print(10)
    exit()
elif n == 3 and m == 0:
    print(100)
    exit()
if c[0][0] == 1 and c[0][-1] == 0:
    print(-1)
    exit()
tmp1 = 0
tmp2 = 0
l1 = []
l2 = []
l3 = []
for i in c:
    if tmp1 == i[0] and tmp2 != i[1]:
        print(-1)
        exit()
    if i[0] == 1:
        l1.append(i)
    if i[0] == 2:
        l2.append(i)
    if i[0] == 3:
        l3.append(i)
    tmp1 = i[0]
    tmp2 = i[1]
if m == 0:
    print(0)
    exit()
l1 = np.array(l1)
l2 = np.array(l2)
l3 = np.array(l3)

if len(l1) != 0:
    l1_min = min(l1[:,1])
else:
    l1_min = 0
if len(l2) != 0:
    l2_min = min(l2[:,1])
else:
    l2_min = 0
if len(l3) != 0:
    l3_min = min(l3[:,1])
else:
    l3_min = 0

if n == 1:
    print(str(l1_min))
elif n == 2:
    if l1_min != 0:
        print(str(l1_min)+str(l2_min))
    else:
        print(str(l1_min+1)+str(l2_min))
else:
    if l1_min != 0:
        print(str(l1_min)+str(l2_min)+str(l3_min))
    else:
        print(str(l1_min+1)+str(l2_min)+str(l3_min))
