#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];
  }
};

using vertex = struct vertex{ll to,cost;vertex(ll to,ll cost):to(to),cost(cost){}};
using edge = struct edge{ll from,to,cost;edge(ll from,ll to,ll cost):from(from),to(to),cost(cost){}};
using weighted_adjlist = vector<vector<vertex>>;
using weighted_edge = vector<edge>;

auto Dijkstra(weighted_adjlist& adj,ll S){
  ll N=(ll)adj.size();
  vector<ll> dist(N,(1LL<<60));
  vector<ll> prev_v(N,-1LL);
  using pll = pair<ll,ll>;
  priority_queue<pll,vector<pll>,greater<>> que;
  dist[S]=0LL;
  que.emplace(dist[S],S);
  while(!que.empty()){
    ll cost,from;
    tie(cost,from) = que.top();
    que.pop();
    if(dist[from]<cost)continue;
    for(const auto& v:adj[from]){
      if( chmin(dist[v.to],dist[from]+v.cost) ){
        prev_v[v.to]=from;
        que.emplace(dist[v.to],v.to);
      }
    }
  }
  return dist;
  //return prev_v;
  //return make_pair(dist,prev_v);
}

main(){

  ll n,m;
  cin>>n>>m;
  weighted_adjlist adj(n);
  rep(i,n-1)adj[i+1].emplace_back(i,0);
  rep(i,m){
    ll l,r,c;
    cin>>l>>r>>c;
    l--;r--;
    adj[l].emplace_back(r,c);
  }

  auto dist = Dijkstra(adj,0);

  cout<<(dist.back()==INF?-1:dist.back())<<endl;

}

void solve(){
  ll n,m;
  cin>>n>>m;
  vector<T3> a(m);
  rep(i,m){
    ll l,r,c;
    cin>>l>>r>>c;
    l--,r--;
    a[i]=make_tuple(r,l,c);
  }
  sort(a.begin(),a.end());

  vector<ll> infv(n,INF);
  segtree<ll> dist(infv,INF,[](auto a,auto b){return min(a,b);});
  dist.update(0,0);
  for(auto const& x:a){
    ll r,l,c;
    tie(r,l,c) = x;
    ll mn = dist.query(l,r+1);
    if(dist[r]>mn+c)dist.update(r,mn+c);
  }
  cout<<(dist[n-1]==INF?-1:dist[n-1])<<endl;

}

Measured memory_bytes: 10872