//
// Created by Ильдар Ялалов on 28.10.2019.
//
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf_int = 1e9 + 100;
const ll inf_ll = 1e18;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double dbl;
#define pb push_back
#define eb emplace_back
const double pi = 3.1415926535898;
#define dout if(debug) cout
#define fi first
#define se second
#define sp setprecision
#define sz(a) (int(a.size()))
#define mp make_pair
#define all(a) a.begin(),a.end()
bool debug = 0;
const int MAXN = 2e5 + 100;
const int LOG = 21;
const int mod = 998244353;
const int MX = (1e7 + 100) * 1.5;
typedef long long li;
const li MOD = 1000000000949747713ll;

template<class T1, class T2>
std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) {
    out << "( " << rhs.first << " , " << rhs.second << " )";
    return out;
}


ll cur_mn[MAXN];
#define left v<<1,tl,tm
#define right v<<1|1,tm+1,tr

ll t[4 * MAXN];

void build(int v, int tl, int tr) {
    t[v] = inf_ll;
    if (tl == tr) {

    } else {
        int tm = (tl + tr) >> 1;
        build(left);
        build(right);
    }
};

void update(int v, int tl, int tr, int l, int r, ll val) {
    if (l > tr || r < tl)
        return;
    if (l <= tl && tr <= r) {
        t[v] = min(t[v], val);
    } else {
        int tm = (tl + tr) >> 1;
        update(left, l, r, val);
        update(right, l, r, val);
    }
}

ll get(int v, int tl, int tr, int pos) {
    if (tl == tr) {
        return t[v];
    } else {
        int tm = (tl + tr) >> 1;
        if (pos <= tm) {
            return min(t[v], get(left, pos));
        } else {
            return min(t[v], get(right, pos));
        }
    }
}


vector<int> start[MAXN];
vector<int> ending[MAXN];

int L[MAXN], R[MAXN], C[MAXN];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int l, r, c;
        cin >> l >> r >> c;
        L[i] = l;
        R[i] = r;
        C[i] = c;
        start[l].pb(i);
        ending[r].pb(i);
    }

    build(1, 1, n);
    update(1, 1, n, 1, 1, 0);

    set<pair<ll, int> > ss;
    int total = 0;
    for (int i = 1; i <= n; ++i) {
        for (int v : ending[i]) {
            ss.erase({cur_mn[v], v});
        }
        ll val = get(1, 1, n, i);
        for (int v : start[i]) {
            cur_mn[v] = val;
            ss.insert({cur_mn[v], v});
            if (val != inf_ll)
                update(1, 1, n, L[v], R[v], C[v] + val);
        }

        if (!ss.empty()) {
            auto it = ss.lower_bound({val + 1, -1});
            vector<int> ids;
            while (it != ss.end()) {
                ids.pb(it->se);
                it = ss.erase(it);
            }
            total+=sz(ids);

            for (int v : ids) {
                cur_mn[v] = val;
                ss.insert({cur_mn[v], v});
                update(1, 1, n, L[v], R[v], C[v] + val);
            }
        }
    }
    dout <<"total "<< total<<endl;
    ll ans = get(1, 1, n, n);
    if (ans != inf_ll) {
        cout << ans << "\n";
    } else {
        cout << "-1" << "\n";
    }

}


signed main() {
#ifdef zxc
    debug = 1;
    freopen("../input.txt", "r", stdin);
    //  freopen("../output.txt", "w", stdout);
#else

#endif //zxc
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout.setf(ios::fixed);
    cout.precision(20);

    int t = 1;
    while (t--)
        solve();
    if (debug)
        cerr << endl << "time : " << (1.0 * clock() / CLOCKS_PER_SEC) << endl;
}

Measured memory_bytes: 22912