#include<iostream>
#include<set>
#include<vector>
using namespace std;
#define ll long long

class P {
public:
  ll L, R, C;
  P(ll _L, ll _R, ll _C): L(_L),R(_R),C(_C) {}
};

struct cmp_L { bool operator()(const P x, const P y) const { return x.L < y.L; } } Cmp_L;
struct cmp_R { bool operator()(const P x, const P y) const { return x.R < y.R; } } Cmp_R;
struct cmp_C { bool operator()(const P x, const P y) const { return x.C < y.C; } } Cmp_C;

int main(){
  ll N, M;
  cin >> N >> M;

  multiset<P, cmp_L> Plist;
  multiset<P, cmp_C> Qlist;
  for(ll m=0; m<M; ++m) {
    ll L, R, C;
    P p(L,R,C);
    Plist.insert(p);
  }

  ll i=1;
  vector<ll> M(N+1);
  M[1]=0;

  while(i<N){
    for(ll ip=0; ip<P.size() && P[ip].L<=i; ++i){
      auto p = P.begin();
      P q(*p);
      q.C += M[q.L];
      Q.insert(q);
      P.erase(p);
    }

    for(auto itr = Q.begin(); itr!=Q.end(); ++itr){
      if(itr->R>i) break;
      Q.erase(itr);
    }

    if(Q.size()==0){ cout << -1 << endl; return 0; }
    
    auto qq = Q.begin();
    for(ll r=i+1; r<qq->R+1; ++r){
      M[r] = qq->C;
    }
    i = r;
  }

  cout << M[N]<<endl;

  return 0;
}
