#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sz(a) (int)(a.size())
const int mod = 998244353;
const int MAX = 100005;
vector<pair<int , int> > lc[MAX];
ll dp[MAX];
vector<pair<int , ll> > DP;
int main()
{	
	int n , m;cin >> n >> m;
	for(int i = 0; i < m; i++)
	{
		int l , r , c; cin >> l >> r >> c;
	
		l--;r--;
		lc[r].pb({l , c});
	}
	dp[0] = 0;
	DP.pb({0 , 0});
	for(int i = 1; i < n; i++)
	{
		dp[i] = mod * (ll) mod;
		for(auto x : lc[i])
		{
			//cout << x.x << " " << x.y << " " << i << endl;
			dp[i] = min(dp[i] , x.y + lower_bound(DP.begin() , DP.end() , mp(x.x , (ll)-mod))->y);
		}
		//cout << dp[i] << endl;
		while(sz(DP) &&DP.back().y >= dp[i])
			DP.pop_back();
		DP.pb({i , dp[i]});
	}
	cout << ((dp[n - 1] == mod * (ll)mod) ? -1 : dp[n - 1]);
		
}
