NN = 17
MI = [1<<100] * ((1<<NN+1)-1)
def update(a, x):
    i = (1<<NN) - 1 + a
    MI[i] = min(MI[i], x)
    while True:
        i = (i-1) // 2
        MI[i] = min(MI[2*i+1], MI[2*i+2])
        if i == 0:
            break

def rangemin(a):
    l = a + (1<<NN)
    r = ((1<<NN+1)-2)
    mi = 1<<100
    while l < r:
        if l%2:
            mi = min(mi, MI[l-1])
            l += 1
        if r%2:
            r -= 1
            mi = min(mi, MI[r-1])
        l >>= 1
        r >>= 1
    return mi

N, M = map(int, input().split())
X = []
for _ in range(M):
    l, r, c = map(int, input().split())
    X.append((l, r, c))

X = sorted(X)
update(1, 0)
for l, r, c in X:
    mi = rangemin(l)
    update(r, mi + c)

ans = MI[(1<<NN) - 1 + N]
print(ans if ans < 1<<100 else -1)
