#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <climits>
#include <bitset>
#include <functional>
#include <numeric>
#include <ctime>
#include <cassert>
#include <cstring>
#include <fstream>
#include <iomanip>

using namespace std;

#define FOR(i, a, b) for(int (i)=(a);(i)<(b);(i)++)
#define IFOR(i, a, b) for(int (i)=(a);(i)<=(b);(i)++)
#define RFOR(i, a, b) for(int (i)=(a);(i)>=(b);(i)--)
#define REP(i, n) FOR((i), 0, (n))

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;


const ll INF = 1e18;

template<class T>
void print_container(T &container, string delim=" ") {
    bool first = true;
    for(auto &t: container){
        if(!first){
            cout << delim;
        }
        first = false;
        cout << t;
    }
    cout << endl;
}

struct Data{
    ll cost;
    int to;
};
bool operator<(const Data& lhs, const Data& rhs){
    if(lhs.cost != rhs.cost)
        return lhs.cost < rhs.cost;
    else{
        return lhs.to > rhs.to;
    }
}


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

    vector<vector<int>> retu(n+1);
    vector<Data> aiueo(n+1);

    vector<Data> dat(m);
    REP(i, m){
        int l, r, c;
        cin >> l >> r >> c;
        l--, r--;
        dat[i] = {r, c};
        retu[l].push_back(i);
        retu[r].push_back(-i);
    }

    set<Data> se;
    set<Data> ng;
    REP(i, n){
        for(auto t: retu[i]){
            if(t > 0){
                se.insert(dat[t]);
            }
            else{
                ng.insert(dat[t]);
            }
        }
        while(!se.empty()){
            auto dd = se.begin();
            if(ng.count(*dd)){
                se.erase(se.begin());
            }
            else
                break;
        }
            aiueo[i] = *(se.begin());
    }

    int now = 0;
    ll cost = 0;
    while(now < n-1){
        auto to = aiueo[now];
        if(to.cost == INF)
{
    cout << -1 << endl;
    return 0;
}
cost += to.cost;
to = to.to;
    }
    cout << cost << endl;
    return 0;
    
}