#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;


ll solve() {
    ll INF = 1LL<<60;
    ll N, M;
    cin >> N >> M;
    vector<ll> L(M), R(M), C(M);
    vector<pii> ix;
    for ( int i = 0; i < M; i++ ) {
        cin >> L[i] >> R[i] >> C[i];
        L[i]--, R[i]--;
        ix.push_back(pii(L[i], i));
    }
    sort(ix.begin(), ix.end());
    priority_queue<pii, vector<pii>, greater<>> q;  //d,r
    ll d = 0;
    int ii = 0;
    vector<ll> dp(N+1, INF);
    q.push(pii(0,0));
    for ( ll l = 0; l <= N-1; l++ ) {
        if ( q.empty() ) return -1;
        dp[l] = min(dp[l], q.top().first);
        while ( ii < M && ix[ii].first == l ) {
            int j = ix[ii].second;
            q.push(pii(dp[l]+C[j],R[j]));
            ii++;
        }
        if ( !q.empty() && q.top().second <= l ) q.pop();
    }
    return dp[N-1];
}

int main() {
    auto ans = solve();
    cout << ans << "\n";
    return 0;
}