#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
constexpr int MOD = 1000000007;
int main()
{
    int N, M;
    cin >> N >> M;
    vector<tuple<int, int, lint>> pair(M);
    REP(i, M) {
        int L, R;
        lint C;
        cin >> L >> R;
        cin >> C;
        pair[i] = make_tuple(L-1, R-1, C);
    }
    sort(pair.begin(), pair.end(),
       [](auto const &a, auto const &b) { return get<0>(a) < get<0>(b); });
    vector<lint> dp(N, 1e15);
    priority_queue<lint, vector<lint>, greater<lint> > pq;
    dp[0] = 0;
    int i = 0;
    REP(k, N) {
        int L = get<0>(pair[i]);
        if(L > k) {
            k++;
            if(pq.empty()) {

                if(dp[N-1] == 1e15) {
                    cout << -1 << endl;
                } else {
                    cout << dp[N-1] << endl;
                }
                return 0;
            }
            lint tmp = pq.top();
            if(dp[k] > tmp) {
                lint min = 1e15;
                int idx = -1;
                FOR(j, k+1, N) {
                    if(dp[j] < min) {
                        min = dp[j];
                        idx = j;
                    }
                    
                } 
                if(idx == -1) {
                    if(dp[N-1] == 1e15) {
                        cout << -1 << endl;
                    } else {
                        cout << dp[N-1] << endl;
                    }
                    return 0;
                }
                if(dp[k] >= min) {
                    FOR(j, k, idx) {
                        dp[j] = min;
                    }
                    
                }
                pq.pop();
            } else if(dp[k] == tmp) {
                pq.pop();
            }

            k--;
            continue;
        }
        //cout << dp[L]+get<2>(pair[i]) << " " << dp[get<1>(pair[i])] << endl;
        if(dp[L]+get<2>(pair[i]) < dp[get<1>(pair[i])]) {
            dp[get<1>(pair[i])] = dp[L]+get<2>(pair[i]);
            pq.push(dp[L]+get<2>(pair[i]));
        }
        k--;
        i++;
        if(i == M) break;
    }
    if(dp[N-1] == 1e15) {
        cout << -1 << endl;
    } else {
        cout << dp[N-1] << endl;
    }
}