// Original C++ solution (slower):
#include<iostream>

#include<string>

#include<cstdio>

#include<vector>

#include<cmath>

#include<algorithm>

#include<functional>

#include<iomanip>

#include<queue>

#include<ciso646>

#include<random>

#include<map>

#include<set>

#include<complex>

#include<bitset>

#include<stack>

#include<unordered_map>

#include<utility>

#include<tuple>

using namespace std;

typedef long long ll;

typedef unsigned int ui;

const ll mod = 1000000007;

const ll INF = (ll)1000000007 * 1000000007;

typedef pair<ll, ll> P;

#define stop char nyaa;cin>>nyaa;

#define rep(i,n) for(int i=0;i<n;i++)

#define per(i,n) for(int i=n-1;i>=0;i--)

#define Rep(i,sta,n) for(int i=sta;i<n;i++)

#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)

#define rep1(i,n) for(int i=1;i<=n;i++)

#define per1(i,n) for(int i=n;i>=1;i--)

#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)

typedef long double ld;

typedef complex<ld> Point;

const ld eps = 1e-8;

const ld pi = acos(-1.0);

typedef pair<ll, ll> LP;



int n,m;ll d[100010]={INF};

set<P> S;

vector<P> seg_start[100010];

vector<int> seg_end[100010];

bool visited[100010];

int c[100010],l[100010],r[100010];



void solve(){

    cin >> n >> m;

    rep(i,m) {

        cin >> l[i] >> r[i] >> c[i];

        seg_start[l[i]-1].push_back(P(i,c[i]));

        seg_end[r[i]-1].push_back(i);

    }

    d[0]=0;

    for(P p:seg_start[0]){

        S.insert(P(d[0]+p.second,p.first));

    }

    Rep(i,1,n){

        //cout << i << endl;

        if (S.empty()) {

            cout << -1 << endl;

            return;

        }

        while(visited[(*S.begin()).second]) {

            S.erase(S.begin());

            if (S.empty()) {

            cout << -1 << endl;

            return;

            }

        }

        

        d[i]=(*S.begin()).first;

        if (d[i]==INF) {

            cout << -1 << endl;

            return;

        }

        for(P p:seg_start[i]){

            S.insert(P(d[i]+p.second,p.first));

        }

        for(int a:seg_end[i]){

            visited[a]=true;

        }

        //cout << i << " " << d[i] << endl;

    }

    cout << d[n-1] << endl;

}



int main(){

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout << fixed << setprecision(50);

    solve();

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<string>

#include<cstdio>

#include<vector>

#include<cmath>

#include<algorithm>

#include<functional>

#include<iomanip>

#include<queue>

#include<ciso646>

#include<random>

#include<map>

#include<set>

#include<complex>

#include<bitset>

#include<stack>

#include<unordered_map>

#include<utility>

#include<tuple>

using namespace std;

typedef long long ll;

typedef unsigned int ui;

const ll mod = 1000000007;

const ll INF = (ll)1000000007 * 1000000007;

typedef pair<ll, int> P;

#define stop char nyaa;cin>>nyaa;

#define rep(i,n) for(int i=0;i<n;i++)

#define per(i,n) for(int i=n-1;i>=0;i--)

#define Rep(i,sta,n) for(int i=sta;i<n;i++)

#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)

#define rep1(i,n) for(int i=1;i<=n;i++)

#define per1(i,n) for(int i=n;i>=1;i--)

#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)

typedef long double ld;

const ld eps = 1e-8;

const ld pi = acos(-1.0);

typedef pair<ll, ll> LP;

int dx[4]={1,-1,0,0};

int dy[4]={0,0,1,-1};



template <typename T>

struct SegmentTree{

  using F = function<T(T,T)>;

  int n;

  F f;//二項演算

  T ti;//単位元

  vector<T> dat;



  SegmentTree(){}

  SegmentTree(F f,T ti):f(f),ti(ti){}



  void init(int n_){//sizeがn_のsegtreeを作る

    n=1;

    while(n<n_) n<<=1;

    dat.assign(n<<1,ti);

  }



  void build(const vector<T> &v){//vによってsegtreeをbuildする

    int n_=v.size();

    init(n_);

    for(int i=0;i<n_;i++) dat[n+i]=v[i];

    for(int i=n-1;i;i--)

      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);

  }



  void set_val(int k,T x){//k番目をxにする

    dat[k+=n]=x;

    while(k>>=1)

      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);

  }



  T query(int a,int b){//区間[a,b)に対しFを適応した値を返す

    if(a>=b) return ti;

    T vl=ti,vr=ti;

    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {

      if(l&1) vl=f(vl,dat[l++]);

      if(r&1) vr=f(dat[--r],vr);

    }

    return f(vl,vr);

  }



  template<typename C>

  int find(int st,C &check,T &acc,int k,int l,int r){//

    if(l+1==r){

      acc=f(acc,dat[k]);

      return check(acc)?k-n:-1;

    }

    int m=(l+r)>>1;

    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);

    if(st<=l&&!check(f(acc,dat[k]))){

      acc=f(acc,dat[k]);

      return -1;

    }

    int vl=find(st,check,acc,(k<<1)|0,l,m);

    if(~vl) return vl;

    return find(st,check,acc,(k<<1)|1,m,r);

  }



  template<typename C>

  int find(int st,C &check){

    T acc=ti;

    return find(st,check,acc,1,0,n);

  }



  T &operator [] (int i) {return dat[i+n];};

};



int n,m;

vector<P> ls[100010];



void solve(){

    cin >> n >> m;

    rep(i,m){

        int l,r;ll c;cin >> l >> r >> c;l--;r--;

        ls[r].emplace_back(c,l);

    }

    SegmentTree<ll> seg([](ll a,ll b){return min(a,b);},INF);

    seg.init(n-1);

    seg.set_val(0,0);

    Rep(i,1,n){

        ll res=INF;

        for(P p:ls[i]){

            int l=p.second;ll c=p.first;

            res=min(res,seg.query(l,i)+c);

        }

        seg.set_val(i,res);

    }

    if(seg[n-1]<INF)cout << seg[n-1] << endl;

    else cout << -1 << endl;

}



int main(){

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout << fixed << setprecision(50);

    solve();

}

// Fastest known solution for this problem: