import sys
def input():
    return sys.stdin.readline()[:-1]
import bisect
N,M=map(int,input().split())
S = [list(map(int, input().split())) for i in range(M)]
from operator import itemgetter
S.sort(key=itemgetter(1))
x=[0]
y=[0]
for i in S:
    t=bisect.bisect_left(x,i[0]-1)
    if t!=len(x):
        x.append(i[1]-1)
        y.append(y[t]+i[2])
    else:
        print(-1)
        break
else:
    print(y[bisect.bisect_left(x,N-1)])