#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <complex>

class LRC{
  public:
  long long int L, R, C;
  
  LRC(long long int l, long long int r, long long int c){
    L = l;
    R = r;
    C = c;
  }

  bool operator < (const class LRC & another) const{
    return L < another.L;
  }

};

long long int INF = 1e18;

// update range [a, b) by min(value, current value)
// the variables should be k = 0, l = 0, r = N_segtree
int update_range(std::vector<long long int> &min_segtree, std::vector<long long int> &lazy, long long int N_segtree, long long int a, long long int b, long long int value, long long int k, long long int l, long long int r);

// get min in range [a, b) 
// the variables should be k = 0, l = 0, r = N_segtree
long long int get_min(std::vector<long long int> &min_segtree, std::vector<long long int> &lazy, long long int N_segtree, long long int a, long long int b, long long int k, long long int l, long long int r);

// subroutines
int propagate_lazy(std::vector<long long int> &segtree, std::vector<long long int> &lazy, long long int index, long long int l, long long int r);


int main(){

  // read problem
  long long int N, M, tmp1, tmp2, tmp3;
  std::vector<class LRC> lrc;
  scanf("%lld%lld", &N, &M);
  for(long long int i = 0; i < M; i++){
    scanf("%lld%lld%lld", &tmp1, &tmp2, &tmp3);
    lrc.push_back(LRC(tmp1-1, tmp2-1, tmp3));
  }
  std::sort(lrc.begin(), lrc.end(), std::less<class LRC>());

  // solve problem
  long long int N_segtree = (1LL<<17); 
  std::vector<long long int> min_segtree(2*N_segtree-1);
  std::vector<long long int> lazy(2*N_segtree-1);
  for(long long int i = 0; i < 2*N_segtree-1; i++){
    min_segtree[i] = INF;
    lazy[i] = INF;
  }

  update_range(min_segtree, lazy, N_segtree, 0, 1, 0, 0, 0, N_segtree);

  for(long long int i = 0; i < M; i++){
    update_range(min_segtree, lazy, N_segtree, lrc[i].L+1, lrc[i].R+1, get_min(min_segtree, lazy, N_segtree, lrc[i].L, lrc[i].L+1, 0, 0, N_segtree) + lrc[i].C, 0, 0, N_segtree);
  }

  // print answer
  long long int ans = get_min(min_segtree, lazy, N_segtree, N-1, N, 0, 0, N_segtree);
  std::cerr << "Answer: " << std::endl;
  if(ans >= INF){
    std::cout << "-1" << std::endl;
  }
  else{
    std::cout << ans << std::endl;
  }

  return 0;




/*
  long long int N_segtree = (1LL<<3); // set appropriately
  std::vector<long long int> min_segtree(2*N_segtree-1);
  std::vector<long long int> lazy(2*N_segtree-1);
  for(long long int i = 0; i < 2*N_segtree-1; i++){
    min_segtree[i] = INF;
    lazy[i] = INF;
  }*/
}


int propagate_lazy(std::vector<long long int> &segtree, std::vector<long long int> &lazy, long long int index, long long int l, long long int r){

  // evaluate
  segtree[index] = std::min(segtree[index], lazy[index]);    
  // if not lowest node
  if(r-l > 1){
    lazy[2*index+1] = std::min(lazy[2*index+1], lazy[index]);
    lazy[2*index+2] = std::min(lazy[2*index+2], lazy[index]);
  }
  lazy[index] = INF;

  return 0;
}

int update_range(std::vector<long long int> &min_segtree, std::vector<long long int> &lazy, long long int N_segtree, long long int a, long long int b, long long int value, long long int k, long long int l, long long int r){  
  // propagate 
  propagate_lazy(min_segtree, lazy, k, l, r);

  // if out of range
  if(b <= l || r <= a){
    return 0;
  }

  // if [a, b) covers [l, r)
  if(a <= l && b >= r){
    lazy[k] = std::min(lazy[k], value);
    propagate_lazy(min_segtree, lazy, k, l, r);

    return 0;
  }

  // calculate child nodes
  else{
    min_segtree[k] = std::min(min_segtree[k], value);

    update_range(min_segtree, lazy, N_segtree, a, b, value, 2*k+1, l, (l+r)/2);
    update_range(min_segtree, lazy, N_segtree, a, b, value, 2*k+2, (l+r)/2, r);

    return 0;
  }
}

long long int get_min(std::vector<long long int> &min_segtree, std::vector<long long int> &lazy, long long int N_segtree, long long int a, long long int b, long long int k, long long int l, long long int r){

  // if [a, b) and [l, r) have no intersection
  if(r <= a || l >= b){
    return INF;
  }

  // propagate
  propagate_lazy(min_segtree, lazy, k, l, r);

  // if [a, b) covers [l, r)
  if(a <= l && b >= r){
    return min_segtree[k];
  }
  // if [a, b) and [l, r) have intersection
  else{
    return std::min(get_min(min_segtree, lazy, N_segtree, a, b, 2*k+1, l, (l+r)/2), get_min(min_segtree, lazy, N_segtree, a, b, 2*k+2, (l+r)/2, r));
  }
}


Measured memory_bytes: 7536