#include <bits/stdc++.h>
#define rep(i,l,r) for (int i=l,i##end=r;i<=i##end;++i)
#define per(i,r,l) for (int i=r,i##end=l;i>=i##end;--i)
#define ll long long
#define pr pair<int,int>
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N=1e6+6; const ll INF=1e18;
vector< pr > G[N];
int st[N],tp;
ll f[N];
int main(){
    int n,m; cin>>n>>m;
    rep(i,1,m){
        int l,r,c; cin>>l>>r>>c;
        G[r].push_back(mp(l,c));
    }
    f[0]=INF,f[1]=0; st[tp=1]=1;
    rep(i,2,n){
        f[i]=INF;
        rep(j,0,G[i].size()-1){
            int l=G[i][j].fi,v=G[i][j].se;
            f[i]=min(f[i],v+f[*lower_bound(st+1,st+1+tp,l)]);
        }
        while (tp&&f[i]<f[st[tp]]) --tp;
        st[++tp]=i;
    }
    cout<<(f[n]==INF?-1:f[n])<<endl;
    return 0;
}
