#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define ALL(v) v.begin(),v.end()
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cout<<v[i]<<" "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<int, int> P;
struct Edge{ll from, to, cost;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod){
  ll res=1;
  while(n>0){
    if(n&1) res=res*a%mod;
    a=a*a%mod;
    n>>=1;
  }
  return res;
}

ll modinv(ll a, ll mod){
  return modpow(a, mod-2, mod);
}

ll gcd(ll a, ll b){
  ll r = a%b;
  if(r==0) return b;
  else return gcd(b, a%b);
}

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}

vector<P> v[100100];
vector<ll> cost;
struct node{
  ll x, y;
  bool friend operator > (node a, node b){
    return a.x>b.x;
  }
};
/**************************************
 ** A main function starts from here  **
 ***************************************/
int main(){
  ll N, M;
  cin >> N >> M;
  cost = llvec(N, 1e18);
  //fill(ALL(cost), 1e16);
  rep(i,N-2){
    v[i+1].push_back({i, 0});
  }
  rep(i, M){
    ll l, r, c;
    cin >> l >> r >> c;
    l--;r--;
    v[l].push_back({r,c});
  }
  
  cost[0]=0;
  //priority_queue<P> que;
  priority_queue<node> que;
  node n;
  n.x=0;
  n.y=0;
  que.push(n);
  while(!que.empty()){
    n =que.top();
    ll from = n.y;
    que.pop();
    if(n.x>cost[from])continue;
    rep(i, v[from].size()){
      ll to = v[from][i].first;
      ll c = v[from][i].second;
      if(cost[to]>cost[from]+c){
        cost[to]=cost[from]+c;
        n.x=cost[to];n.y=to;
        que.push(n);
      }
    }
  }
  
  cout << (cost[N-1]==1e16?-1:cost[N-1]);

  return 0;
}
