import sys
from heapq import *
input = sys.stdin.readline

def main():
    n, m = map(int, input().split())
    
    tree = [[] for _ in range(n)]
    for i in range(m):
        s, t, c = map(int, input().split())
        tree[t-1].append((s-1, c))
        tree[s-1].append((t-1, c))
    
    for i in range(n-1):
        tree[i+1].append((i, 0))
    
    ans = [-1]*n
    already = [False]*n
    not_yet = [(0, 0)]
    
    while not_yet:
        value, key = heappop(not_yet)
        if already[key]:
            continue
        already[key] = True
        ans[key] = value
        
        for k, v in tree[key]:
            if already[k]:
                continue
            heappush(not_yet, (v+value, k))
        
    print(ans[n-1])
    
    
    
    
if __name__ == "__main__":
    main()
