import sys
from heapq import heappop, heappush

def main():
    input = sys.stdin.readline
    INF = 10**15
    N, M = map(int, input().split())
    heap = []
    for _ in range(M):
        l, r, c = map(int, input().split())
        heappush(heap, (l-1, r-1, c))
    
    dp = [INF]*N
    dp[0] = 0
    while heap:
        l, r, c = heappop(heap)
        
        s = min(dp[l:r])
        dp[r] = min(dp[r], s+c)
        
    if dp[N-1] == INF:
        return -1
    
    return dp[N-1]

if __name__ == '__main__':
    print(main())