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

#define int long long

typedef pair<int, int> P;

int dist[100010];

vector<P> vec[100010];

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

    priority_queue<P, vector<P>, greater<P>> que;

    for(int i = 0;i < m;i++){
        int l, r, c;
        cin >> l >> r >> c;
        l--;
        r--;

        vec[l].push_back({r, c});
    }


    memset(dist, -1, sizeof(dist));

    dist[0] = 0;
    for(auto e : vec[0]){
        que.push({e.second, e.first});
    }


    int now = 0;
    while(!que.empty()){
        int cost = que.top().first;
        int r = que.top().second;
        que.pop();
        if(now < r){
            for(int i = now+1;i <= r;i++){
                dist[i] = cost;
                for(auto e : vec[i]){
                    que.push({e.second+cost, e.first});
                }
            }
            now = r;
        }
    }

    cout << dist[n-1] << endl;

    return 0;
}



