#include <bits/stdc++.h>

#define long long long int
using namespace std;

// @author: pashka

int main() {
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;
    vector<int> l(m), r(m), c(m);
    vector<vector<int>> beg(n);
    vector<vector<int>> end(n);
    for (int i = 0; i < m; i++) {
        cin >> l[i] >> r[i] >> c[i];
        l[i]--;
        r[i]--;
        beg[l[i]].push_back(i);
        end[r[i]].push_back(i);
    }
    vector<long> d(n, LONG_MAX);
    d[0] = 0;
    set<pair<long, int>> now;
    for (int i = 0; i < n; i++) {
        if (i > 0) {
            if (now.empty()) {
                cout << -1;
                return 0;
            }
            d[i] = now.begin() -> first;
//            cout << i << " " << d[i] << "\n";
        }
        for (int x : beg[i]) {
//            cout << i << " " << x << " " << d[l[x]] + c[x] << "\n";
            now.insert({d[l[x]] + c[x], x});
        }
        for (int x : end[i]) {
            now.erase({d[l[x]] + c[x], x});
        }
    }
    cout << d[n - 1] << "\n";

    return 0;
}