#include <bits/stdc++.h>
using namespace std;

int main(){
  int N,M;
  cin>>N>>M;
  vector<tuple<int,int,int64_t>> p(M+1);
  for(int i=0;i<M;i++){
    int a,b;
    int64_t c;
    cin>>a>>b>>c;
    a--,b--;
    p[i]=make_tuple(a,b,c);
  }
  p[M]=make_tuple(167167,167167,167167);
  sort(p.begin(),p.end());
  vector<int64_t> l(N);
  priority_queue<int64_t,vector<int64_t>,greater<int64_t>> pq;
  priority_queue<int64_t,vector<int64_t>,greater<int64_t>> pqo;
  pq.push(0);
  int a=0;
  vector<vector<int64_t>> G(N);
  G[0].push_back(0);
  for(int i=0;i<N;i++){
    l[i]=pq.top();
    if(i==N-1){
      break;
    }
    while(get<0>(p[a])==i){
      int64_t D=get<2>(p[a])+l[i];
      G[get<1>(p[a])].push_back(D);
      pq.push(D);
      a++;
    }
    for(auto x:G[i]){
      pqo.push(x);
    }
    while(pq.top()==pqo.top()&&!pq.empty()&&!pqo.empty()){
      pq.pop();
      pqo.pop();
    }
    if(pq.empty()){
      l[N-1]=-1;
      break;
    }
  }
  cout<<l[N-1]<<endl;
    
}