#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e5+5,inf=1e18;
ll l[N],r[N],c[N],dp[N];
vector<ll> ent[N],ext[N];
int main(){
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll n,m;cin>>n>>m;
    for(ll i=1;i<=m;i++){
        cin>>l[i]>>r[i]>>c[i];
        ent[r[i]].push_back(i);
        ext[l[i]].push_back(i);
    }
    for(ll i=1;i<n;i++)dp[i]=inf;
    multiset< pair<ll,ll> > st;
    for(ll i=n;i>=1;i--){
        if(!st.empty())dp[i]=min(dp[i] , st.begin()->first);
        for(auto &id:ent[i])st.insert({dp[r[id]]+c[id],id});
        for(auto &id:ext[i])st.erase({dp[r[id]]+c[id],id});
    }
    if(dp[1]>=inf)cout<<"-1";
    else cout<<dp[1];
}