#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(ll i=0;i<n;i++)
const ll INF=1e15;
const ll MOD=1000000007;

int main(){
    ll N,M;cin>>N>>M;
    vector<pair<ll,ll>> info[N];
    rep(i,M){
        ll L,R,C;cin>>L>>R>>C;
        L--;R--;
        info[L].push_back(make_pair(R,C));
    }
    rep(i,N-1)info[i+1].push_back(make_pair(i,0));
    ll sum[N];
    rep(i,N){
        if(!i)sum[i]=0;
        else sum[i]=INF;
    }
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> que;
    que.push(make_pair(0,0));
    while(que.size()){
        ll d=que.top().first,v=que.top().second;
        que.pop();
        for(auto i:info[v]){
            ll nv=i.first;
            ll nc=i.second;
            if(d+nc<sum[nv]){
                sum[nv]=d+nc;
                que.push(make_pair(sum[nv],nv));
            }
        }
    }
    if(sum[N-1]==INF)cout<<-1<<endl;
    else cout<<sum[N-1]<<endl;
    return 0;
}