#include <iostream>
#include <queue>
using namespace std;
typedef long long  ll;
typedef pair<ll,ll> pii;
const ll maxn = 1e6+10;
const ll inf = 1e14;
ll N,M;
ll h[maxn],e[maxn],ne[maxn],w[maxn],idx;
ll dis[maxn];

priority_queue<pii,vector<pii>,greater<pii> > heap;

void add(ll a,ll b,ll c){
    e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++;
}

ll Dijkstra(){
    heap.push({0,1});
    dis[1] = 0;
    
    while (heap.size()) {
        auto cur = heap.top();heap.pop();
        ll d = cur.first,s = cur.second;
        for(ll i = h[s];i!=-1;i = ne[i]){
            ll j = e[i];
            if(d+w[i] < dis[j]){
                dis[j] = d+w[i];
                heap.push({dis[j],j});
            }
        }
    }
    if(dis[N] == inf ) return -1;
    return dis[N];
}

void init(){
    for(ll i = 1;i<=N;i++) dis[i] = inf;
    memset(h,-1,sizeof(h));
    for(ll i = 1;i<=N;i++){
        add(i,i-1,0);
    }
}

int main(){
    cin>>N>>M;
    init();
    ll a,b,c;
    for(ll i = 0;i<M;i++){
        scanf("%lld%lld%lld",&a,&b,&c);
        add(a,b,c);
    }
    cout<<Dijkstra()<<endl;
    return 0;
}
