#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define debug(x) cerr<<#x<<": "<<x<<endl
#define all(x) x.begin(), x.end()
using ll = long long;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<int> l(m), r(m);
    vector<ll> c(m);
    rep(i,0,m) {
        cin >> l[i] >> r[i] >> c[i];
        --l[i];
        --r[i];
    }
    vector<vector<pair<int,ll>>> g(n);
    rep(i,1,n) g[i].emplace_back(i-1,0);
    rep(i,0,m) g[l[i]].emplace_back(r[i],c[i]);
    const ll INFTY = LLONG_MAX;
    vector<ll> dist(n,INFTY);
    using T = pair<ll,int>;
    priority_queue<T,vector<T>,greater<T>> pq;
    pq.emplace(0,0);
    while(!pq.empty()) {
        ll d;
        int u;
        tie(d,u) = pq.top();
        pq.pop();
        if(d<dist[u]) {
            dist[u] = d;
            for(const auto &e:g[u]) {
                pq.emplace(d+e.second, e.first);
            }
        }
    }
    ll ans = dist[n-1];
    if(ans==INFTY) ans = -1;
    cout<<ans<<endl;
}
