#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
const ll INF = 1e15;

typedef pair<ll,ll> P;
struct edge{
    int to,cost;
};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int v,n;
    cin >> v >> n;
    ll d[v];
    vector<vector<edge>> G(v);
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        a--; b--;
        G[a].push_back({b,c});
    }
    rep(i,v) if(i+1<v) G[i+1].push_back({i,0});
    priority_queue<P,vector<P>,greater<P>> pq;
    rep(i,v) d[i]=INF;
    d[0]=0;
    pq.push(P(0,0));
    while(!pq.empty()){
        P p=pq.top();
        pq.pop();
        int c = p.second;
        if(d[c]<p.first) continue;
        rep(i,G[c].size()){
            edge e = G[c][i];
            if(d[e.to] > d[c]+e.cost){
                d[e.to] = (ll)(d[c] + e.cost);
                pq.push({d[e.to],e.to});
            }
        }
    }
    
    if(d[v-1]<INF) cout << d[v-1] << endl;
    else cout << -1 << endl;
}

