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

tree_list = [[] for _ in range(N) ]
for _ in range(M):
    L,R,C = map(int,input().split(' '))
    tree_list[R-1].append([C,L-1])
    #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
current_num = -1
flag = False

for i in reversed(range(N)):
    if dp[i] == 1000000000:
        flag = True
        break
    current_start_paths = tree_list[i]
    if len(current_start_paths) > 0:
        current_start_paths.sort()
        for path in current_start_paths:
            if dp[path[1]] > dp[i] + path[0]:
                for j in range(path[1],i):
                    if (dp[i] + path[0]) < dp[j]:
                        dp[j] = dp[i] + path[0]
                    else:
                        break 

'''
for path in tree_list:
    if dp[path[0]] == 1000000000:
        flag = True
        break
    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 or flag:
    print(-1)
else:
    print(dp[0])