#include <bits/stdc++.h>

using namespace std;
inline void prep ()
{
  cin.tie (0);
  cin.sync_with_stdio (0);
};
const double pi = acos(-1.);
//printf(%.12lf\n,mid);
// __builtin_popcount(int) count 1's in binary
// get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;

long long mymod(long long num){
  if (num< 0 && !(num%mod == 0)){
    if (mod == 1){
      return 0;
    }
    return mod-abs(num)%mod;
  }else{
    return num%mod;
  }
}

/*string sentence = And I feel fine...;
    istringstream iss(sentence);
*/  

double eps = .000000001;
const long long maxx = 1000000000000000000LL;
int main(){
  prep();
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > rl[n+1];
  for (int i=0; i<m; i++){
      int l, r, v;
      cin >> l >> r >> v;
      rl[r].push_back({l, v});
  }

  set<int> have;
  have.insert(1);
  long long vals[n+1];
  bool got[n+1] = {false};
  got[1] = true;
  vals[1] = 0;
 
  for (int i=2; i<=n; i++){
      long long curval = maxx;
      for (int j=0; j<rl[i].size(); j++){
          int l = rl[i][j].first;
          int val = rl[i][j].second;
          set<int>::iterator it = have.lower_bound(l);
          if (it == have.end()){
              continue;
          }
          long long sumplus = vals[*it]+val;
          if (sumplus >= curval){
              continue;
          }
          curval = sumplus;
          set<int>::iterator it2 = have.end();
          it2--;
          while (have.size() > 0 && vals[*it2] > sumplus){
              have.erase(it2);
          }
      }
      
      if (curval != maxx){
          have.insert(i);
          got[i] = true;
          vals[i] = curval;
      }
  }
  if (!got[n]){
      cout << -1;
  }else{
      cout << vals[n];
  }
  return 0; 
}