#include<algorithm>
#include<iostream>
#include<vector>
using ll=long long;
using namespace std;
int main(){
  ll inf=1e18;
  int n,m;
  cin>>n>>m;
  ll d[n];
  d[0]=0;
  for(int i=1;i<n;++i)d[i]=inf;
  vector<vector<int> >a(m,vector<int>(3));
  for(int i=0;i<m;++i){
    int l,r,c;
    cin>>l>>r>>c;
    a[i][0]=l-1;
    a[i][1]=r-1;
    a[i][2]=c;
  }
  sort(a.begin(),a.end());
  for(int i=0;i<m;++i){
    int l=a[i][0],r=a[i][1],c=a[i][2];
    for(int j=l;j<r;++j){
      ll t=d[j]+c;
      if(t<d[r])d[r]=t;
    }
  }
  cout<<(d[n-1]==inf?-1:d[n-1])<<'\n';
}