#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 100001;

int n, m;
vector <pair <int, int> > v[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 0 ; i < m ; i++){
        int l, r, c;
        cin >> l >> r >> c;
        v[l].push_back(make_pair(i, c));
        v[r].push_back(make_pair(i, -1));
    }
    multiset <ll> s;
    set <pair <int, ll> > g;
    ll dp = 0;
    for(int i = 1 ; i <= n ; i++){
        if(i > 1){
            if(s.size() == 0) finish(-1);
            else dp = *s.begin();
        }
        for(auto &j : v[i]){
            if(j.second != -1){
                s.insert(dp + j.second);
                g.insert(make_pair(j.first, dp + j.second));
            }
            else{
                auto it = g.lower_bound(j);
                s.erase(s.find((*it).second));
                g.erase(it);
            }
        }
    }
    cout << dp << endl;
}
