#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
ll n,t[200010],mx = 10000000000000000;
ll dp[100010];
vector<pair<int,ll>> v[100010];
void built(){
	for(int i = n-1;i>0;i--){
		t[i] = min(t[i<<1],t[i<<1|1]);
	}
}

void update(ll p,ll a){
	for(t[p+=n] = a;p>1;p >>= 1){
		t[p>>1] = min(t[p],t[p^1]);
	}
}

ll query(int l, int r){
	ll mn = mx;
	for(l += n, r += n; l<r; l >>= 1,r >>= 1){
		if(l&1) mn = min(mn,t[l++]);
		if(r&1) mn = min(mn,t[--r]);
	}
	return mn;
}

int main(){
	int i,j,m;
	cin >> n >> m;
	for(i=0;i<m;i++){
		int l,r,c;
		cin >> l >> r >> c;
		l--; r--;
		v[r].push_back({l,c});
	}
	for(i=0;i<n;i++){
		dp[i] = mx;
		t[i + n] = mx;
	}
	built();
	dp[0] = 0;
	update(0,0);
	for(i=1;i<=n;i++){
		for(j=0;j<v[i].size();j++){
			dp[i] = min(dp[i],query(v[i][j].first,i) + v[i][j].second);
		}
		update(i,dp[i]);
	}
	if(dp[n - 1]!=mx){
		cout << dp[n - 1] << endl;
	}else{
		cout << -1 << endl;
	}
}