#include <bits/stdc++.h>
using namespace std;

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const int INF = 1 << 30;
const ll INFL = 1LL << 60;
using pli = pair<ll, int>;
class Solver {
  public:
    bool solve() {
        int N, M; cin >> N >> M;
        map<int, vector<pii>> LRC;
        rep(i, M) {
            int l, r; ll c; cin >> l >> r >> c;
            l--; r--;
            LRC[l].push_back(pii(r, c));
        }
        priority_queue<pli, vector<pli>, greater<pli>> que;
        bool fail = false;
        for(pii p : LRC[0]) {
            que.push(pli(p.second, p.first));
        }
        repeat(i, 1, N-1) {


            if(not LRC.count(i)) continue;
            while(que.size() and que.top().second < i) {
                que.pop();
            }
            if(que.size() == 0) {
                fail = true;
                break;
            }
            ll now = que.top().first;
            for(pii p : LRC[i]) {
                que.push(pli(now + p.second, p.first));
            }
        }
        while(que.size() and que.top().second < N-1) {
            que.pop();
        }
        if(fail or que.size() == 0) cout << -1 << endl;
        else cout << que.top().first << endl;
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}
~/D/P