def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #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())

cost = [[float("inf") for i in range(n)] for i in range(n)] 
#cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(m):
    x,y,z = map(int,input().split())
    
    for j in range(x+1,y+1):
        #print(x,j,z)
        
        if cost[x-1][j-1] != 0:
            cost[x-1][j-1] = min(z,cost[x-1][j-1])
        else:
            cost[x-1][j-1] = z
            
        if cost[j-1][x-1] != 0:
            cost[j-1][x-1] = min(z,cost[j-1][x-1])
        else:
            cost[j-1][x-1] = z
        
ans = dijkstra(0,n,m,cost)[n-1]
if ans == float("inf"):
    print(-1)
else:
    print(ans)