#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

#define int long long
#define edge pair<int,int>
#define x first
#define v second

const int N=1e5+5;
int st[N][20],n,m,f[N];
vector<edge> g[N];

void update(int x){
	st[x][0]=f[x];
	for(int i=1;x>(1<<i-1);i++) st[x][i]=min(st[x][i-1],st[x-(1<<i-1)][i-1]);
}

int que(int l,int r){
	int k=log2(r-l+1);
	return min(st[r][k],st[l+(1<<k)-1][k]);
}


signed main(){
	read(n,m);
	for(int i=1,x,y,v;i<=m;i++){
		read(x,y,v);
		g[y].emplace_back(x,v);
	}
	for(int i=2;i<=n;i++){
		update(i-1);
		f[i]=1e15;
		for(auto e:g[i]) f[i]=min(f[i],e.v+que(e.x,i-1));
	}
	if(f[n]==1e15) f[n]=-1;
	write(f[n]);
}