#include<iostream>
#include<vector>
#include<algorithm>
#include<tuple>
using namespace std;
#define INF 1000000000000
#define SZ 131072

typedef vector<pair<pair<int,int>,long long int>> Graph;

struct Segtree{
  long long int data[2*SZ-1];
  
  Segtree(){
    int n = SZ;
    for(int i=0;i<2*n-1;i++) data[i] = INF;
    data[n-1] = 0;
    for(int i=n-2;i>=0;i--) data[i] = min(data[2*i+1],data[2*i+2]);
  }
  
  void change(int id, long long int val){
  	int i = id + SZ - 1;
    data[i] = val;
    i = i/2;
    while(1){
    	data[i] = min(data[2*i+1],data[2*i+2]);
      	if(i==0) break;
    	i = i/2;
    }
  }
  
  long long int find(int a, int b,int k=0, int l=0, int r=SZ){
  	if(a<=l && r<=b) return data[k];
    if(b <= l || r <= a) return INF;
    else {
    	return min(find(a, b, 2*k+1, l,(l+r)/2), find(a,b, 2*k+2, (l+r)/2, r));
    }
  }
};

int main(){
  int n,m; 
  cin >> n >> m;
  Segtree segt();
  Graph g(m);
  for(int i=0;i<m;i++){
  	cin >> g[i].first.first >> g[i].first.second >> g[i].second;
  }
  sort(g.begin(), g.end());
  for(int i=0;i<m;i++){
  	int l = g[i].first.first-1, r = g[i].first.second , c = g[i].second;
  	long long int mc = segt.find(l,r-1);
    long long int minc = segt.find(r-1,r);
  //  cout << minc << endl;
    segt.change(r-1, min(minc,mc+c));
  }
//  for(int i=0;i<n;i++) cout << segt.data[SZ+i-1] << " ";
  if(segt.find(n-1,n) == INF) cout << - 1 << endl;
  else cout << segt.find(n-1,n) << endl;
  return 0;
}
  
