#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

struct edge {
    ll to, cost;
    edge(ll a, ll b) {
        to = a;
        cost = b;
    }
};

// distance,number
using P = pair<ll, ll>;

int main() {
    ll n, m;
    cin >> n >> m;
    vector<vector<edge>> g(n);
    ll zero_memo[n] = {};
    rep(i, m) {
        ll l, r, c;
        cin >> l >> r >> c;
        l--;
        r--;
        g[l].push_back(edge(r, c));
        zero_memo[l]++;
        zero_memo[r]--;
    }

    ll sum = 0;
    rep(i, n - 1) {
        sum += zero_memo[i];
        if (sum > 0) {
            g[i + 1].push_back(edge(i, 0));
        }
    }

    ll d[n];
    fill(d, d + n, mod);
    priority_queue<P, vector<P>, greater<P>> q;
    d[0] = 0;
    q.push(P(0, 0));
    while (!q.empty()) {
        P p = q.top();
        q.pop();
        ll v = p.second;
        if (d[v] < p.first)
            continue;
        rep(i, g[v].size()) {
            edge e = g[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                q.push(P(d[e.to], e.to));
            }
        }
    }

    if (d[n - 1] == mod) {
        cout << -1 << endl;
        return 0;
    }
    cout << d[n - 1] << endl;

    return 0;
}

