import pandas as pd
import sys

N, M = map(int, input().split())
L = []
R = []
C = []
for m in range(M):
    l,r,c = map(int, input().split())
    L.append(l)
    R.append(r)
    C.append(c)

data = pd.DataFrame({"L":L, "R":R, "C":C})
position = 1
min_c = 0
while True:
    data_tmp = data[(data["L"] <= position) & (data["R"] > position)]
    if len(data_tmp)==0:
        print(-1)
        sys.exit()
        
    min_c_tmp = data_tmp["C"].min()
    min_c += min_c_tmp
    position = data_tmp[data_tmp["C"]==min_c_tmp]["R"].max()

    if position == N:
        print(min_c)
        break