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

using namespace std;
typedef long long ll;

const ll INF = 1145141919810364364;

int main() {
    int n, m;
    cin >> n >> m;

    vector<vector<ll>> paths(m, vector<ll>(3));
    priority_queue<vector<ll>, vector<vector<ll>>, greater<>> que;
    vector<ll> minCost(n + 1, INF);
    minCost[1] = 0;
    for (int i = 0; i < m; i++) {
        int l, r;
        ll c;
        cin >> l >> r >> c;
        paths[i] = {l, r, c};
    }

    sort(paths.begin(), paths.end());
    int pos = 0;
    for (int i = 1; i <= n; i++) {
        while (pos < m && paths[pos][0] == i) {
            que.push({paths[pos][1], paths[pos][2]});
            pos++;
        }
        while (!que.empty() && que.top()[0] < i)que.pop();

        vector<vector<ll>> trash(que.size());
        int tmp = 0;
        while (!que.empty()) {
            vector<ll> path = que.top();
            que.pop();
            trash[tmp] = path;
            tmp++;
            minCost[path[0]] = min(minCost[path[0]], minCost[i] + path[1]);
        }
        for (auto x:trash)que.push(x);
    }

    cout << (minCost[n] == INF ? -1 : minCost[n]) << endl;
    return 0;
}