N,M = map(int,input().split(' '))

tree_list = [[] for _ in range(N) ]
LRC = sorted([list(map(int, input().split())) for i in range(M)], key=lambda x: (x[0], x[1]))
inf = float('inf')
dp = [inf] * N
dp[0] = 0
flag = False
if LRC[0][0] != 1:
    print(-1) 
else:
    for l, r, c in LRC:
        l = l -1
        r = r -1
        '''
        if dp[l] == 1000000000000:
            flag = True
            break
        '''
        if dp[r] > dp[l] + c:
            for i in reversed(range(l+1,r+1)):
                if dp[i] > dp[l] + c:
                    dp[i] = dp[l] + c
                else:
                    break 
    if dp[N-1] < inf:
        print(dp[N-1])
    else:
        print(-1)
        