#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll start = 1;
ll n;
vector<vector<pair<ll, ll>>> G;
vector<ll> dist;
void dijkstra() {
    priority_queue<pi, vector<pi>, greater<pi>> pq;
    dist = vector<ll>(n+1,INF);
    dist[start] = 0;
    pq.push(pi(0, start));

    while(!pq.empty()) {
        int d = pq.top().first, v = pq.top().second;
        pq.pop();

        if(v == n) break;
        if(dist[v] < d) continue;

        for(int i = 0; i < (int)G[v].size(); ++i) {
            pi e = G[v][i];

            if(d + e.second < dist[e.first]) {
                dist[e.first] = d + e.second;
                pq.push(pi(dist[e.first], e.first));
            }
        }
    }
}

int main() {
    ll m, i, j, k;
    cin >> n >> m;
    vector<pair<pair<ll, ll>, ll>> v(m);
    for(i = 0; i < m; i++) {
        cin >> v[i].first.second >> v[i].second >> v[i].first.first;
    }
    sort(v.begin(), v.end());
    G = vector<vector<pair<ll,ll>>> (n+1);
    dist.resize(n+1);
    set<pair<ll,ll>> st;
    ll ok[n+2]={};
    for(i=0;i<m;i++){
        if(st.find({v[i].first.second, v[i].second})!=st.end())continue;
        G[v[i].first.second].push_back({v[i].second, v[i].first.first});
        st.insert({v[i].first.second,v[i].second});
        //ok[v[i].first.second] = 1;
        //ok[v[i].second] = -1;
    }
    /*for(i=1;i<n+1;i++){
        ok[i]+=ok[i-1];
        if(ok[i]<=0){
            cout << -1 << endl;
            return 0;
        }
    }*/
    
    for(i=1;i<=n;i++){
        G[i].push_back({i-1,0});
    }
    dijkstra();
    if(dist[n]==INF){
        cout << -1 << endl;
        return 0;
    }
    cout << dist[n] << endl;
    return 0;
}