// Slower C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>



using namespace std;



const long long INF = (1LL << 60);

const int MAX_N = 1<<17;

long long segVal[2*MAX_N-1];

int segUpdate[2*MAX_N-1];



void change(int l, int r, long long val, int idx = 0, int rangeL = 0, int rangeR = MAX_N){

	if(rangeR <= l || r <= rangeL) return;

	if(l <= rangeL && rangeR <= r){

        segVal[idx] = val;

        segUpdate[idx] = 1;

        // cout << "c" << segVal[idx] << " " << segUpdate[idx] << " " << idx << endl;

	} else {

		int rangeM = (rangeL+rangeR)/2;

		change(l, r, val, 2*idx+1, rangeL, rangeM);

		change(l, r, val, 2*idx+2, rangeM, rangeR);

	}

}



long long getVal(int l, int r, int idx = 0, int rangeL = 0, int rangeR = MAX_N){

	if(rangeR <= l || r <= rangeL) return INF;

	if(l <= rangeL && rangeR <= r){

        // cout << "r" << segVal[idx] << " " << idx << " " << rangeL << " " << rangeR << endl;

        return segVal[idx];

	} else {

        // cout << "u" << idx << " " << rangeL << " " << rangeR << endl;

        // cout << segVal[idx] << " " << segUpdate[idx] << endl;

        int rangeM = (rangeL+rangeR)/2;

        if(segUpdate[idx]){

            // cout << "Update" << endl;

            change(rangeL, rangeM, segVal[idx], 2*idx+1, rangeL, rangeM);

            change(rangeM, rangeR, segVal[idx], 2*idx+2, rangeM, rangeR);

            segUpdate[idx] = 0;

        }

        return min(getVal(l, r, 2*idx+1, rangeL, rangeM), getVal(l, r, 2*idx+2, rangeM, rangeR));

	}

}



long long getVal(int idx){

    return getVal(idx, idx+1);

}



int main(){

    int N, M;

    while(cin >> N >> M){

        for(int i=0;i<2*MAX_N-1;i++) segUpdate[i] = 0;

        change(0, N, INF);

        vector<pair<pair<int,int>, long long>> Q(M);

        for(auto& t : Q){

            cin >> t.first.first >> t.first.second >> t.second;

            --t.first.first;

            --t.first.second;

        }

        sort(Q.begin(), Q.end());

        // cout << "test " << segUpdate[32767] << endl; 

        // cout << getVal(0) << endl; return 0;

        for(int i=0;i<N;i++) getVal(i);

        change(0, 1, 0);

        // for(int i=0;i<N;i++) cout << getVal(i) << " "; cout << endl;

        for(auto& q : Q){

            // cout << q.first.first << " " << q.first.second << " " << q.second << endl;

            long long next = getVal(q.first.first) + q.second;

            if(getVal(q.first.second) <= next) continue;

            int L = q.first.first, R = q.first.second;

            while(R-L > 1){

                int mid = (L+R)/2;

                if(getVal(mid) > next) R = mid;

                else L = mid; 

            }

            // cout << R << endl;

            change(R, q.first.second+1, next);

            // for(int i=0;i<N;i++) cout << getVal(i) << " "; cout << endl;

        }

        long long res = getVal(N-1);

        cout << (res == INF ? -1 : res) << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 2.056854419670197x):
#include <iostream>

#include <vector>

#include <algorithm>



using namespace std;



const long long INF = 1LL << 60;



template<typename T>

class RangeUpdateQuery {

public:

    explicit RangeUpdateQuery(int n, T def) : N(calcN_(n)) {

        mVal.assign(2*N+1, make_pair(-1, def));

    }

    void update(int l, int r, T value, int ts){

        updateImpl_(l, r, make_pair(ts, value), 0, 0, N);

    }

    T get(int idx){

        int i = N + idx - 1;

        auto res = mVal[i];

        while(i > 0){

            i = (i-1)/2;

            res = max(mVal[i], res);

        }

        return res.second;

    }

private:

    int calcN_(int n){

        int res = 1;

        while(res < n) res *= 2;

        return res;

    }

    void updateImpl_(int l, int r, pair<int, T> value, int idx, int rangeL, int rangeR){

        if(r <= rangeL || rangeR <= l) return;

        if(l <= rangeL && rangeR <= r){

            mVal[idx] = value;

        } else {

            int rangeM = (rangeL+rangeR)/2;

            updateImpl_(l, r, value, 2*idx+1, rangeL, rangeM);

            updateImpl_(l, r, value, 2*idx+2, rangeM, rangeR);

        }

    }

    const int N;

    vector<pair<int,T>> mVal; 

};



int main(){

    int N, M;

    while(cin >> N >> M){

        vector<pair<pair<int,int>, int>> vp(M);

        for(auto& p : vp){

            cin >> p.first.first >> p.first.second >> p.second;

            --p.first.first;

            --p.first.second;

        }

        sort(vp.begin(), vp.end());

        RangeUpdateQuery<long long> ruq(N, INF);

        ruq.update(0, 1, 0, 0);

        for(int i=0;i<M;i++){

            auto& p = vp[i];

            auto next = ruq.get(p.first.first) + p.second;

            if(ruq.get(p.first.second) <= next) continue;

            int L = p.first.first, R = p.first.second;

            while(R-L > 1){

                int mid = (L+R)/2;

                if(ruq.get(mid) > next) R = mid;

                else L = mid;

            }

            ruq.update(R, p.first.second+1, next, i+1);

        }

        auto res = ruq.get(N-1);

        cout << (res == INF ? -1 : res) << endl;

    }

}