#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 100010
const ll inf=0x3f3f3f3f3f3f3f3fll;
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int n,m;
ll dp[N],mn[N][19];
vector<pair<int,int> > vec[N];
void Insert(int x){
	mn[x][0]=dp[x];
	for(int i=1;i<=18;++i){
		if(x<=(1<<i-1))break;
		mn[x][i]=min(mn[x][i-1],mn[x-(1<<i-1)][i-1]);
	}
}
ll Query(int l,int r){
	int k=log2(r-l+1);
	return min(mn[r][k],mn[l+(1<<k)-1][k]);
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=m;++i){
		int l=read(),r=read(),c=read();
		vec[r].emplace_back(l,c);
	}
	memset(mn,0x3f,sizeof(mn));
	memset(dp,0x3f,sizeof(dp));
	dp[1]=0;
	Insert(1);
	for(int i=2;i<=n;++i){
		for(auto x:vec[i]){
			dp[i]=min(dp[i],Query(x.first,i-1)+x.second);
		}
		Insert(i);
	}
	printf("%lld\n",dp[n]==inf?-1:dp[n]);
	return 0;
}
