#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int,int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define RREP(i,n) for(int i=(n)-1;i>=0;--i)
#define GOODBYE do { cout << "No" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl

signed main(void){
    int N,M;
    vector<pair<pair<int,int>,int>> RLC;
    cin >> N >> M;
    REP(i,M){
        int r,l,c;
        cin >> l >> r >> c;
        r--;l--;
        RLC.push_back({{r,l},c});
    }
    sort(RLC.begin(),RLC.end(),greater<>());
    vector<int> G;
    int d[114514];
    int e[114514]={};
    int k=0;
    REP(i,M){
        //G.insert({RLC[i].first.second,INF});
        
        if(e[RLC[i].first.second]==0){
            G.push_back(RLC[i].first.second);
            e[RLC[i].first.second]=1;
            d[k]=INF;
            k++;
        }
    }
    //G[N-1]=0;
    G.push_back(N-1);
    d[k]=0;
    sort(G.begin(),G.end());
    //REP(i,G.size()){cout << G[i]+1 << " ";}cout << endl;
    
    REP(i,RLC.size()){
        int r=RLC[i].first.first,l=RLC[i].first.second,c=RLC[i].second;
        //cout << l+1 MM r+1 MM c << "    ";
        decltype(G)::iterator it = upper_bound(G.begin(), G.end(), r);
        decltype(G)::iterator it2 = lower_bound(G.begin(), G.end(), l);
        it--;
        int L=it2-G.begin();
        //cout << it-G.begin() << "  ";
        d[L]=min(d[L],d[it-G.begin()]+c);
        //cout <<  l+1 MM d[L] << endl;
        
    }
    if(d[0]==INF)d[0]=-1;
    cout << d[0] << endl;
    return 0;
}

