#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)


int main(){
    ll N, M; cin >> N >> M;
    vector<vector<pair<ll,ll>>> G(N+1);
    rep(i, 0, M){
        ll v, u, w;
        cin >> v >> u >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
    }
    rep(i, 1, N) {
        G[i+1].push_back({i, 0});
    }

    ll srt = 1;
    constexpr ll INF = 1e18;
    vector<ll> d(N+1, INF);
    
    /* dijkstra's scope */{
        priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> que;
        que.push({0, srt});
        d[srt] = 0;
        while(!que.empty()){
            auto q = que.top();
            que.pop();
            if(q.first > d[q.second]) continue;
            for(auto g : G[q.second]){
                if(d[g.first] <= q.first + g.second) continue;
                d[g.first] = q.first + g.second;
                que.push({d[g.first], g.first});
            }
        }
    }

    if(d[N] == INF) cout << -1 << endl;
    else cout << d[N] << endl;
    return 0;
}