#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef tuple<int, ll, int> edges;

int main()
{
    int N, M;
    cin >> N >> M;
    set<edges> edgeSet;
    for(int i = 0 ; i < M; i ++){
        int L, R;
        ll C;
        cin >> L >> R >> C;
        edgeSet.insert(edges(L, C, R));
    }
    auto ite = edgeSet.begin();
    ll INF = LLONG_MAX;
    vector<ll> dp(N + 1, INF);
    dp.at(1) = 0;
    while (ite != edgeSet.end()) {  
        edges edge = *ite;
        int left =  get<0>(edge);
        ll cost =  get<1>(edge);
        int right = get<2>(edge);
        if(dp.at(left) == INF){
            ite++;
            continue;
        }
        ll currentDistance = cost + dp.at(left);
        for(int i = right; i >= left; i--){
            if(dp.at(i) <= currentDistance){
                break;
            }
            dp.at(i) = currentDistance;
        }
        ite++;
    }
    if(dp.at(N) == INF){
        cout << -1 << endl;
    }else{
        cout << dp.at(N) << endl;
    }
    return 0;
}