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

tree_list = []
for _ in range(M):
    L,R,C = map(int,input().split(' '))
    tree_list.append([R-1,L-1,C])
    '''
    if (R-1) in tree_list[L-1].keys():
        tree_list[L-1][R-1] = min(tree_list[L-1][R-1],C)
    else:
        tree_list[L-1][R-1] = C
    '''
tree_list.sort(reverse=True)
dp = [1000000000] * N
dp[N-1] = 0
for path in tree_list:
    if dp[path[1]] > dp[path[0]] + path[2]:
        for i in range(path[1],path[0]):
            if (dp[path[0]] + path[2]) < dp[i]:
                dp[i] = dp[path[0]] + path[2]
            else:
                break 
if dp[0] == 1000000000:
    print(-1)
else:
    print(dp[0])