import numpy as np
def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d


n, m = map(int, input().split())
ro = [[float("inf") for i in range(n)] for i in range(n)]
lis = np.array([[int(i) for i in input().split(" ")] for i in range(m)])
lis = lis[lis[:,0].argsort(), :]
used = [[False for i in range(n)] for i in range(n)]
for i in lis:
    for j in range(i[0]-1, i[1]-1):
        for k in range(i[0], i[1]):
            if used[j][k] == False:
                ro[j][k] = i[2]
                ro[k][j] = i[2]
                used[j][k] = True
cost = ro         
te = dijkstra(0,n,n-1,cost)
if te[-1] == float("inf"):
    print(-1)
else:
    print(te[-1])