#include <bits/stdc++.h>
#define int long long
using namespace std;
using PII = pair<int, int>;
const int INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

struct Edge {
    int to;
    int cost;
};

signed main() {
    int n, m;
    cin >> n >> m;
    vector<vector<Edge>> g(n);
    for (int i = 0; i < m; i++) {
        int l, r, c;
        cin >> l >> r >> c;
        g[l - 1].push_back(Edge{r - 1, c});
    }
    for (int i = 1; i < n; i++) {
        g[i].push_back(Edge{i - 1, 0});
    }

    priority_queue<PII, vector<PII>, greater<PII>> q;
    vector<int> d(n, INF);
    d[0] = 0;
    q.push({0, 0});
    while(!q.empty()) {
        PII p = q.top();
        q.pop();
        if (d[p.second] < p.first) continue;
        for (auto&& e : g[p.second]) {
            if (d[e.to] > d[p.second] + e.cost) {
                d[e.to] = d[p.second] + e.cost;
                q.push({d[e.to], e.to});
            }
        }
    }

    cout << (d[n - 1] == INF ? -1 : d[n - 1]) << "\n";
}