#include <bits/stdc++.h>

using namespace std;
#define int long long
#define x first
#define y second

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

mt19937 rand1(time(0));
int randlr(int l, int r) {
    return rand1() % (r - l + 1) + l;
}

vector<pii> g[100009];
int dist[100009];

const int inf = 1e17;

signed main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    while (m--) {
        int l, r, c;
        cin >> l >> r >> c;

        g[l - 1].push_back({r - 1, c});
    }

    for (int i = 1; i < n; i ++) {
        g[i].push_back({i - 1, 0});
        dist[i] = inf;
    }

    priority_queue<pii> q;
    q.push({0, 0});
    while (q.size()) {
        auto vp = q.top();
        q.pop();

        if (-vp.x != dist[vp.y]) continue;
        int v = vp.y;

        for (auto u : g[v]) {
            if (dist[u.x] > dist[v] + u.y) {
                dist[u.x] = dist[v] + u.y;
                q.push({-dist[u.x], u.x});
            }
        }
    }

    if (dist[n - 1] == inf) dist[n - 1] = -1;

    cout << dist[n - 1];

    return 0;
}


