#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,int>P;
const int N=1e5+5,M=131072;
int n,m,l[N],r[N],c[N],i;
bool b[N];
vector<int>t[N*3];
void ins(int l,int r,int x){
	for(l+=M-1,r+=M+1;l^r^1;l>>=1,r>>=1){
		if(!(l&1))t[l^1].push_back(x);
		if(r&1)t[r^1].push_back(x);
	}
}
set<int>s;
priority_queue<P,vector<P>,greater<P>>q;
inline void work(int x,ll d){
	vector<int>ve;
	for(x+=M;x;x>>=1)ve.insert(ve.end(),t[x].begin(),t[x].end()),t[x].clear();
	for(int x:ve)if(!b[x])b[x]=1,q.push({d+c[x],x});
}
int main(){
	scanf("%d%d",&n,&m);
	for(i=1;i<=m;++i)scanf("%d%d%d",l+i,r+i,c+i),ins(l[i],r[i],i);
	for(i=1;i<=n;++i)s.insert(i);
	work(1,0);
	for(;!q.empty();){
		ll d;int x;tie(d,x)=q.top();q.pop();
		if(r[x]==n){printf("%lld\n",d);return 0;}
		for(auto it=s.lower_bound(l[x]);it!=s.end() && *it<=r[x];s.erase(it++))work(*it,d);
	}
	puts("-1");
	return 0;
}