#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a) for(int i=0;i<(a);i++)
const ll MOD=1000000007;
//const ll MOD=998244353;

int N,M;
ll L[101010],R[101010],C[101010];
vector<pair<ll,int>> V;
set<pair<ll,int>> st;
ll d[101010];
const ll INF=1e15;

int main(){
  cin>>N>>M;
  V.resize(M);
  rep(i,M){
    cin>>L[i]>>R[i]>>C[i]; L[i]--,R[i]--;
    V[i]={L[i],i};
  }
  sort(V.begin(),V.end());
  V.push_back({1e9,0});
  rep(i,N) d[i]=INF;
  d[0]=0;
  int ind=0;
  rep(i,N){
    while(st.size()){
      auto it=st.begin();
      if(it->second<=i){
        st.erase(it);
      }else{
        d[i]=min(d[i],it->first);
        break;
      }
    }
    if(d[i]==INF){
      cout<<-1<<endl;
      return 0;
    }
    while(V[ind].first==i){
      int j=V[ind++].second;
      if(d[R[j]]>d[i]+C[j]){
        d[R[j]]=d[i]+C[j];
        st.insert({d[R[j]],R[j]});
      }
    }
  }
  cout<<d[N-1]<<endl;
  return 0;
}