from bisect import bisect_left

N, M = map(int, input().split())
LRC = []

for _ in range(M) :
    L, R, C = map(int, input().split())
    LRC.append((L-1, R-1, C))
    
LRC.sort()
LRC.sort(key=lambda x: x[1])

cost_r = [0]
cost_v = [0]
for L, R, C in LRC :
    i = bisect_left(cost_r, L)
    if i == len(cost_r) :
        continue
    nc = cost_v[i] + C
    
    while nc < cost_v[-1] :
        cost_r.pop()
        cost_v.pop()
    
    if R > cost_r[-1] :
        cost_r.append(R)
        cost_v.append(nc)
    
if cost_r[-1] == N - 1 :
    print(cost_v[-1])
else :
    print(-1)