/*
the top of the losers
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define    Lcm(a,b)  ((a*b)/(__gcd(a,b)))
#define    X            real()
#define    Y            imag()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
#define    dp(a,b)          (((conj(a))*(b)).real())
#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     point                    complex<double>
#define     point        complex<double>
#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    PI  acos(-1)
typedef  long long     ll ;
typedef  unsigned long long     ull ;
const double eps= (1e-9);
using namespace std;
using namespace __gnu_pbds;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);}
int countNumBit1(int mask) {int ret=0; while (mask) 	{mask &= (mask-1);++ret;	}return ret;}
void GO(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
const int N=100000+9;
const ll inf=1e16;
multiset<pair<int,int> > st;
vector< pair<int,int> > v[N];
int a[N],b[N],c[N];
ll dist[N];
bool vis[N];
vector< vector< pair<int,int> > > g(N);
int dijk(int s,int ta){

   for(int i=1;i<=100000;i++){
    dist[i]=inf;
   }
   priority_queue< pair<ll,int> ,vector<pair<ll,int> > ,greater< pair<ll,int> > > pq;
   pq.push(mp(0,1));
   dist[1]=0;

   while(sz(pq)>0){
    int cur=pq.top().second;
    int add=pq.top().first;
    pq.pop();
    if(vis[cur]) continue;
    vis[cur]=1;

    for(int i=0;i<sz(g[cur]);i++){
        int nw=g[cur][i].first;
        int co=g[cur][i].second;
        if(dist[nw]>add+co){
            dist[nw]=add+co;
            pq.push(mp(add+co,nw));
        }
    }
   }
   if(dist[ta]==inf) return -1;
   return dist[ta];
}
int main(){
    GO();
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>a[i]>>b[i]>>c[i];
        //v[a[i]].pb(mp(0,i));
        //v[b[i]].pb(mp(1,i));
        g[a[i]].pb(mp(b[i],c[i]));
        g[b[i]].pb(mp(a[i],c[i]));
    }
    for(int i=n;i>=2;i--){
        g[i].pb(mp(i-1,0));
    }

    cout<<dijk(1,n)<<endl;
}


















