#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

struct bb {
    ll a;
    ll b;
    ll c;
};

int main() {
    ll n, m;
    cin >> n >> m;
    vector<bb> imp;
    ll result[n+1];
    result[1] = 0;
    for(ll i=2;i<=n;i++) {
        result[i] = -1;
    }
    for(ll i=0; i < m;i++){
        ll a, b, c;
        cin >> a >> b >> c;
        bb sg = {a, b,c};
        imp.push_back(sg);
    }
    sort(imp.begin(), imp.end(), [](const bb& x, const bb& y){return x.a < y.a ||( x.a == y.a && (x.b < y.b || ( x.b == y.b && x.c < y.c))) ;} );
    for(vector<bb>::const_iterator i = imp.begin(); i != imp.end(); i++) {
        if (result[i->a] == -1) {
            cout << -1 << endl;
            return 0;
        }
        ll sh = result[i->a] + i->c;
        if (result[i->b] != -1 && result[i->b] <= sh) {
            continue;
        }
        for(ll j = i->a; j <= i->b; j++) {
            if (result[j] == -1 || result[j] > sh) {
                result[j] = sh;
            }
        }
    }
    cout << result[n] << endl;
}