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)
l1 = []
l2 = []
l3 = []
for i in c:
    if i[0] == 1:
        l1.append(i)
    if i[0] == 2:
        l2.append(i)
    if i[0] == 3:
        l3.append(i)

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(-1)
else:
    if l1_min != 0:
        print(str(l1_min)+str(l2_min)+str(l3_min))
    else:
        print(-1)