#include<bits/stdc++.h>
using namespace std; 
#define MOD 1000000007
#define f(i, n) for(ll i=0; i < (n); i++)
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<int, int> P;
const ll mk = 1e5 ; 
int main(){
  int N , M ; cin >> N >> M ;
  vector<vector<P> > v(N);
  vector<ll> cost(N, 1e18);
  f(i,N-2){
    v[i+2].push_back(make_pair(i+1, 0));
  }
  f(i, M){
    ll l, r, c;
    cin >> l >> r >> c;
    l--;r--;
    v[l].push_back(make_pair(r,c));
  }
  
  cost[0]=0;
  priority_queue<P, vector<P>, greater<P> > Queue;
  llvec check(N, 0);
  Queue.push(make_pair(0, 0));
  while(!Queue.empty()){
    P p =Queue.top();
    Queue.pop();
    ll from = p.second;
    
    if(check[from]!=0)continue;
    check[from]=1;
    ll vnum = v[from].size();
    f(i, vnum){
      ll to = v[from][i].first;
      ll c = v[from][i].second;
      if(cost[to]>cost[from]+c){
        cost[to]=cost[from]+c;
        Queue.push(make_pair(cost[to], to));
      }
    }
  }
  
  cout << (cost[N-1]==1e18?-1:cost[N-1]);
  
  return 0;
}