#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int N_MAX = 100001;
long long INF=123456789123456789;
typedef pair<int,long long> P;
typedef pair<P,int> PP;
int main()
{
  int n,m,l,r,i;
  long long c;
  long long res[N_MAX];
  PP pp;
  priority_queue<PP,vector<PP>,greater<PP>> q;
  cin >> n >> m;
  res[1]=0;
  fill(res+2,res+n+1,INF);
  for(i=0;i<m;i++){
    cin >> l >> r >> c;
    q.push(PP(P(l,c),r));
  }
  pp=q.top();
  if(pp.first.first>1){
    cout << "-1" << endl;
    return 0;
  }
  while(!q.empty()){
    pp=q.top(); q.pop();
    l=pp.first.first;
    c=pp.first.second;
    r=pp.second;
    for(i=r;i>l;i--)
      if(res[l]+c<res[i]){
        res[i]=res[l]+c;
      }else{
        break;
      }
  }
  if(res[n]==INF)
    cout << "-1" << endl;
  else
    cout << res[n] << endl;
  return 0;
}