/* 
 Author:N_o_o_B 
 Created:November 09 2019 17:50:03
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}
 
template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}
 
template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<vl> vvl;
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define fori(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define ford(i,n)  for(int i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define fi first
#define se second
clock_t time_p=clock();
void time_taken()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
//const ll mod=998244353;
const ll mod=1e9+7;
const ll INF=1e18;
const int maxn=1e5+5;
vector<pair<ll,ll>> seg(4*maxn,{INF,INF});
vl lazy(4*maxn,INF);
void build(int v,int tl,int tr){
    if(tl>tr) return;
    if(tl==tr){
        seg[v].se=tl;
        return;
    }
    int mid=(tl+tr)/2;
    build(2*v,tl,mid);
    build(2*v+1,mid+1,tr);
    seg[v]=min(seg[2*v],seg[2*v+1]);
}
void upd(int v,int tl,int tr,int pos,ll val){
    if(pos > tr or pos < tl) return;
    if(pos==tl and tl==tr) {seg[v].fi=val;seg[v].se=tl;return;}
    for(int x:{2*v,2*v+1}){
        seg[x].fi=min(seg[x].fi,lazy[v]);
        lazy[x]=min(lazy[x],lazy[v]);
    }
    lazy[v]=INF;
    int mid=(tl+tr)/2;
    upd(2*v,tl,mid,pos,val);
    upd(2*v+1,mid+1,tr,pos,val);
    seg[v]=min(seg[2*v],seg[2*v+1]);
}
void upd2(int v,int tl,int tr,int ql,int qr,ll val){
    if(tl > qr or tr < ql) return;
    if(tl>=ql and tr<=qr) {
        seg[v].fi=min(seg[v].fi,val);
        lazy[v]=min(lazy[v],val);
        return;
    }
    for(int x:{2*v,2*v+1}){
        seg[x].fi=min(seg[x].fi,lazy[v]);
        lazy[x]=min(lazy[x],lazy[v]);
    }
    lazy[v]=INF;
    int mid=(tl+tr)/2;
    upd2(2*v,tl,mid,ql,qr,val);
    upd2(2*v+1,mid+1,tr,ql,qr,val);
    seg[v]=min(seg[2*v],seg[2*v+1]);
}
pair<ll,ll> query(int v,int tl,int tr,int ql,int qr){
    if(tl>qr or tr < ql) return {INF,INF};
    if(tl>=ql and tr<=qr) return seg[v];
    for(int x:{2*v,2*v+1}){
        seg[x].fi=min(seg[x].fi,lazy[v]);
        lazy[x]=min(lazy[x],lazy[v]);
    }
    lazy[v]=INF;
    int mid=(tl+tr)/2;
    auto p1=query(2*v,tl,mid,ql,qr);
    auto p2=query(2*v+1,mid+1,tr,ql,qr);
    return min(p1,p2);
}
typedef pair<pair<int,int>,ll> pil;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cout.precision(12);cout << fixed;
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<pil> a;
    fori(i,m){
        int l,r;
        ll c;
        cin >> l >> r >> c;
        a.pb({{l,r},c});
    }
    build(1,1,n);
    sort(all(a));
    upd(1,1,n,1,0);
    for(auto p:a){
        int l=p.fi.fi,r=p.fi.se;
        ll d=p.se;
        pair<ll,ll> mn=query(1,1,n,l,r);
        upd2(1,1,n,l,r,d+mn.fi);
    }
    ll ans=query(1,1,n,n,n).fi;
    if(ans==INF) ans=-1;
    cout << ans << endl;
    time_taken();
    return 0;
}

Measured memory_bytes: 13296