#include <algorithm>
#include <iostream>
#include <vector>

using ll = long long;
using namespace std;

int main() {
    ll inf = 1e18;
    int n, m;
    cin >> n >> m;
    ll d[n];
    d[0] = 0;
    for (int i = 1; i < n; ++i) d[i] = inf;
    vector<vector<int>> a(m, vector<int>(3));
    for (int i = 0; i < m; ++i) {
        int l, r, c;
        cin >> l >> r >> c;
        a[i][0] = l-1;
        a[i][1] = r-1;
        a[i][2] = c;
    }
    sort(a.begin(), a.end());
    for (const auto& e : a) {
        int l = e[0], r = e[1], c = e[2];
        for (int j = l; j < r; ++j) {
            ll t = d[j] + c;
            if (t < d[r]) d[r] = t;
        }
    }
    if (d[n-1] == inf) cout << -1 << endl;
    else cout << d[n-1] << endl;
}