N,M = map(int,input().split())
m = [[0],[0],[0]]
tg = [0,0,0]
for i in range(M):
    s,c = map(int,input().split())
    if m[s-1] == [0] and tg[s-1] == 0:
        m[s-1].pop(0)
        m[s-1].append(c)
        tg[s-1] = 1
    else:
        m[s-1].append(c)

# print(m)
# print(tg)
mr = list(reversed(m))

# print(mr)

if min(mr[N-1]) == 0:
    print(-1)
else:
    print(min(m[0])*10**2+min(m[1])*10+min(m[2]))