#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX_N 200000
#define INF 100000000000000
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
struct edge{
  ll to;
  ll cost;
};
vector<edge> G[MAX_N];
ll d[MAX_N];
signed main(){
  ll n,m,l,r,c;
  priority_queue<P,vector<P>,greater<P>> que;
  cin >> n >> m;
  for(int i=0;i<m;i++){
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].push_back({r,c});
    G[r].push_back({l,c});
  }
  for(int i=1;i<n;i++){
    G[i].push_back({i-1,0});
    d[i] = INF;
  }
  d[0]=INF;
  que.push(P(0,0));
  while(!que.empty()){
    P tmp = que.top();
    que.pop();
    ll v,dis;
    v = tmp.second;
    dis = tmp.first;
    if(d[v]<=dis){
      continue;
    }
    d[v] = dis;
    for(int i=0;i<G[v].size();i++){
      que.push({dis+G[v][i].cost,G[v][i].to});
    }
  }
  if(d[n-1]==INF) d[n-1]=-1;
  cout << d[n-1]<<endl;
}