#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
template<class T>inline bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
inline void dump(){cout<<endl;}
template<class Head,class... Tail>inline void dump(Head&& head, Tail&&... tail){cout<<head<<", ";dump(forward<Tail>(tail)...);}
template<typename T>inline istream &operator>>(istream&input,vector<T>&v){for(auto &elemnt:v)input>>elemnt;return input;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}
using T3 = tuple<ll,ll,ll>;
using T4 = tuple<ll,ll,ll,ll>;

template<class T>
class segtree {
private:
  ll n=1;
  T unit;
  std::vector<T> dat;
  std::function<T(T,T)> func;
public:
  segtree(){}
  segtree(std::vector<T>& a,T v,std::function<T(T,T)> f){
    build(a,v,f);
  }
  void build(std::vector<T>& a,T v,std::function<T(T,T)> f){
    ll sz=a.size();
    unit=v;
    func=f;
    while(n<sz)n<<=1LL;
    dat.resize(2*n-1,unit);
    for(int i=0;i<sz;++i)dat[i+n-1]=a[i];
    for(int i=n-2;i>=0;--i){
      dat[i]=func(dat[i*2+1],dat[i*2+2]);
    }
  }
  void update(ll idx,ll val){
    idx+=n-1;
    dat[idx]=val;
    while(idx){
      idx--;idx>>=1LL;
      dat[idx]=func(dat[idx*2+1],dat[idx*2+2]);
    }
  }
  T query(ll a,ll b,ll k=0,ll l=0,ll r=-1){
    if(r<0)r=n;
    if( b<=l || r<=a )return unit;
    if( a<=l && r<=b )return dat[k];
    else{
      auto left=query(a,b,2*k+1,l,(l+r)/2);
      auto right=query(a,b,2*k+2,(l+r)/2,r);
      return func(left,right);
    }
  }
  T operator[](ll i)const{
    return dat[i+n-1];
  }
};


main(){

  ll n,m;
  cin>>n>>m;
  vector<T3> a,b,c;
  vector<T4> d;
  rep(i,m){
    ll l,r,c;
    cin>>l>>r>>c;
    l--;r--;
    a.emplace_back(r,c,l);
    d.emplace_back(l-r,l,c,r);
  }
  sort(a.begin(),a.end());
  sort(d.begin(),d.end());

  vector<ll> infv(n,INF);
  segtree<ll> dist(infv,INF,[](auto a,auto b){return min(a,b);});
  dist.update(0,0);
  for(int i=1;i<n;++i){
    T3 key(i,0,0);
    auto itr = lower_bound(a.begin(),a.end(),key);
    if(itr==a.end())continue;
    ll l,r,c;
    tie(r,c,l) = *itr;
    ll d = dist.query(l,r+1);
    dist.update(i,d+c);
  }
  ll ans = dist[n-1];
  cout<<(ans>=1e15?-1:ans)<<endl;
  

}