#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define pint pair<int,int>
#define pll pair<ll,ll>
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=998244353 ;

struct edge{int to,cost;};
int V;
vector<edge> G[100010];
ll d[100010];

void dijkstra(int s){
  priority_queue<pint,vector<pint>,greater<pint>> que;
  fill(d,d+V,longinf);
  d[s]=0;
  que.push(pint(0,s));
  ll cnt=0;
  while(!que.empty()){
    cnt++;
    if(cnt>=200020){
      cout << -1 << endl;
      return 0;
    }
    pint p=que.top(); que.pop();
    int v=p.second;
    if(d[v]<p.first)continue;
    rep(i,G[v].size()){
      edge e=G[v][i];
      if(d[e.to]>d[v]+e.cost){
        d[e.to]=d[v]+e.cost;
        que.push(pint(d[e.to],e.to));
      }
    }
  }
}

int main(){
  int n,m;
  cin >> n >> m;
  V=n;
  rep(i,m){
    int x,y,z;
    cin >> x >> y >> z;
    x--,y--;
    G[x].push_back({y,z});
  }
  for(int i=1;i<n;i++){
    G[i].push_back({i-1,0});
  }
  dijkstra(0);
  if(d[n-1]>=longinf)d[n-1]=-1;
  cout << d[n-1] << endl;
return 0;}