// Original C++ solution (slower):
/*

Though leaves are many, the root is one;

Through all the lying days of my youth

I swayed my leaves and flowers in the sun,

Now may I wither into the truth.



- William Butler Yeats

*/

#include <iostream>

#include <algorithm>

#include <math.h>

#include <string.h>

#include <cstdio>

#include <vector>

#include <set>

#include <cassert>

#include <cstdlib>

#include <complex>

#include <cctype>

#include <cmath>

#include <ctime>

#include <deque>

#include <queue>

#include <stack>

#include <map>

#include <set>

#include <sstream>

#include <functional>

#include <iomanip>

#include <bitset>

//#include <windows.h>  //Should be deleted when using AtCoder&POJ

using namespace std;



#define ll long long

#define pii pair<int,int>

#define qi ios::sync_with_stdio(0)

/**==Info==

*Program:1

*Problem:Shortest Path on a Line

*Date:2019-11-11

*Algorithm:My Set

*Status:AC*/



bool debug=false;



template<typename T> struct pq_small{

	priority_queue<T,vector<T>,greater<T> > pq;

	void push(T x){

		pq.push(x);

	}



	void pop(){

		pq.pop();

	}



	int size(){

		return pq.size();

	}



	bool empty(){

		return pq.empty();

	}



	T top(){

		return pq.top();

	}

};



int n,m;



vector<pair<int,ll>> ra[100005];



ll dis[100005];



pq_small<pair<int,ll>> kill;

multiset<ll> val;



const ll inf=1e18;



int main(int argc,char* argv[]){

	qi;

	cin>>n>>m;

	for(int i=0;i<m;i++){

		int a,b,c;

		cin>>a>>b>>c;

		a--;b--;

		ra[a].push_back(make_pair(b,c));

	}

	

	for(int i=0;i<n;i++){

		

//		cout<<"Current at "<<i<<endl;

//		for(int j:val){

//			cout<<j<<" ";

//		}

//		cout<<endl;

		

		//value

		if(i==0){

			dis[i]=0;

		}else{

			if(val.empty()){

				dis[i]=inf;

			}else{

				dis[i]=*val.begin();	

			}

			

		}

		

		//remove

		while(!kill.empty()){

			pair<int,ll> last=kill.top();

			if(last.first==i){

				val.erase(val.find(last.second));

//				val.erase(last.second);

				kill.pop();

//				cout<<"Remove:"<<last.second<<endl;

			}else{

				break;

			}

		}

		

		//shoot the bullet

		for(pii x:ra[i]){

			ll v=0ll+x.second+dis[i];

//			cout<<"INsert "<<x.first<<" "<<v<<endl;

			kill.push(make_pair(x.first,v));

			val.insert(v);

		}

		

		

	}

	

	if(dis[n-1]>=inf){

		cout<<"-1";

	}else{

		cout<<dis[n-1]<<endl;	

	}

	

	return 0;

}




// Optimized solution to the same problem (faster):
#include <iostream>

#include <stdio.h>

#include <algorithm>

#include <queue>

#include <set>

#include <cstdio>

#include <vector>

#include <cstring>

#include <math.h>

#include <iomanip>

#include <bitset>

#include <map>

#include <stack>

#include <cmath>

#define LL long long 

using namespace std; 

 

#define read(x) scanf("%d",&x)

#define Read(x,y) scanf("%d%d",&x,&y)

#define sRead(x,y,z)  scanf("%d%d%d",&x,&y,&z)

#define gc(x)  scanf(" %c",&x);

#define  INF (ll)1e14//无穷设置的大着点

#define mmt(x,y)  memset(x,y,sizeof x)

#define write(x) printf("%d\n",x)

const int N = 11000;

const int M=  2e5+5;

using namespace std;

typedef long long ll;

struct Edge

{

    int next;

    int to;

    ll dis;

}edge[M*2];

int head[M],tot;

ll d[M];bool vis[M];

inline void add(int from,int to,ll dis){

    edge[++tot].next = head[from];

    edge[tot].to = to;

    edge[tot].dis = dis;

    head[from] = tot;

}

struct node

{

    ll val;int id;

    node(){}

    node(int a,ll b):id(a),val(b){}

    bool operator <(node A)const{

        return val > A.val;

    }

};

void dij(int u)

{

    mmt(vis,0);

    d[u] = 0;

    priority_queue<node> Q;

    Q.push({u,0});

    node tmp;

    while(Q.size()){

        tmp = Q.top();

        Q.pop();

        int x = tmp.id;

        if(vis[x]) continue;

        for(int i =  head[x];~i;i = edge[i].next){

            int y = edge[i].to;

            ll dis = edge[i].dis;

            if(d[y] > d[x] +dis){

                d[y] = d[x] + dis;

                Q.push({y,d[y]});

            }

        }

    }

}

inline void init(int n)

{

    mmt(head,-1);

    tot = 0;

    for(int i = 1;i <= n;++i) d[i] = INF;

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    int n,m;

    cin>>n>>m;

    init(n);

    int u,v;ll dis;

    for(int i = 1;i <= n;++i){//加反向边

        add(i,i - 1,0);

    }

    for(int i = 1;i <= m;++i){//只把端点 加进去

        cin>>u >> v>>dis;

        add(u,v,dis);

    }

    dij(1);

    if(d[n] == INF) cout<<-1<<endl;

    else cout<<d[n]<<endl;

}


// Fastest known solution for this problem: