INF = 10 ** 15
MOD = 10 ** 9 + 7 
from heapq import heappop,heappush,heapify
def dijkstra(G,start = 0):
    N = len(G)
    dist = [INF] *N
    dist[start] = 0
    q = [(0,start)]

    while q:
        d,v = heappop(q)
        if d > dist[v]:
            continue
        for e,c in G[v]:
            if dist[v] + c < dist[e]:
                dist[e] = dist[v] + c
                heappush(q,(dist[e],e))
    return dist[-1]

def main():
    N,M = map(int,input().split())
    G = [[] for _ in range(N)]
    for _ in range(M):
        l,r,c = map(int,input().split())
        l -= 1
        r -= 1
        G[l].append((r,c))
        G[r].append((l,c))
    for i in range(N - 1):
        G[i + 1].append((i,0))
    ans = dijkstra(G)
    print(ans if ans < INF else -1)
if __name__ == '__main__':
    main()