#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
// #include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef vector<vl> mt;
typedef unordered_map<ll,unordered_map<ll,ll>> graph;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<string> vs;
typedef vector<vs> table;
typedef vector<vl> v2;
typedef vector<v2> v3;
typedef vector<v3> v4;


const ll e5 = 1 << 20;
const ll mods[] = {998244353, 1000000007, 1000000009};
const ll bases[] = {997, 1009, 1013};
// const ll mod = mods[0];
const ll e3 = 1 << 13;
const ll INF = 1ll << 50;

template<class T>
class segment_tree {
private:
  ll n;
  T d;
  vector<T>* v;
  T _query(ll a,ll b,ll k,ll l,ll r){
    if(r <= a || b <= l) return d;
    if(a <= l && r <= b) return this->v->at(k);
    else {
      T vl = _query(a,b,k*2+1,l,(l+r)/2);
      T vr = _query(a,b,k*2+2,(l+r)/2,r);
      return func(vl,vr);
    }
  }
public:
  segment_tree (){}
  segment_tree(ll n,T d = INF){
    this->n = 1;
    while(n > this->n) this->n <<= 1;
    v = new vector<T>(this->n*2,d);
    this->d = d;
  }
  void update(ll i,T x){
    i += n-1;
    this->v->at(i) = x;
    while(i > 0){
      i = (i-1)/2;
      this->v->at(i) = func(this->v->at(i*2+1),this->v->at(i*2+2));
    }
  }
  T query(ll a,ll b){
    return _query(a,b,0,0,n);
  }
  T func(T a,T b){
    return min(a,b);
  }
};

ll n,m;
v2 lrc;


int main(){
  cin >> n >> m;
  segment_tree<ll> sg(n+1);
  sg.update(1,0);
  lrc = v2(m,vl(3));
  for(ll i = 0;i < m;i++){
    cin >> lrc[i][0] >> lrc[i][1] >> lrc[i][2];
  }
  sort(lrc.begin(),lrc.end());
  for(ll i = 0;i < m;i++){
    ll c = sg.query(lrc[i][0], lrc[i][1]+1);
    ll x = sg.query(lrc[i][1], lrc[i][1]+1);
    sg.update(lrc[i][1], min(x,c+lrc[i][2]));
  }
  if(sg.query(n,n+1) == INF) cout << -1 << endl;
  else cout << sg.query(n,n+1) << endl;


}


Measured memory_bytes: 7680