//#pagma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
#define pf push_front
#define mp(a, b) make_pair(a, b)
#define ret(a) {cout << a; exit(0);}

using namespace std;

mt19937 mrand(random_device{}()); 
const ll mod= 998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

typedef pair < int , int > pii ;
typedef pair < long long  , long long > pll ;
const int N = 1e5 + 233;
int n , m;
vector < pii > ln[N];
ll dp[N] , t[N << 2];
void upd(int v, int tl , int tr , int pos){
	if(tl == tr)
		return void(t[v] = dp[pos]);
	int tm = (tl + tr) >> 1;
	if(pos <= tm)
		upd(v << 1, tl , tm , pos);
	else upd(v << 1|1, tm + 1, tr, pos);
	t[v] = min(t[v << 1] , t[v << 1|1]);	
}
int get(int v, int tl, int tr , int l , int r){
	if(l > r or l > tr or tl > r)
		return 1e15;
	if(l <= tl and tr <= r)
		return t[v];
	int tm = (tl + tr) >> 1;
	return min(get(v << 1 , tl , tm , l, r), get(v << 1|1, tm + 1 ,tr , l , r));
}
main(){
	scanf("%lld%lld" , &n , &m);
	for(int i = 1;  i <= n; i++){
		int l, r ,c;
		scanf("%lld%lld%lld" , &l , &r , &c);
		ln[r].pb({l , c});		
	}
	for(int i = 2; i <= n; i++)
		dp[i] = 1e15, upd(1 , 1 , n , i);

	upd(1 , 1 , n , 1);

	for(int i = 2 ; i <= n ; i++){
		int cur = 1e15;
		for(auto x : ln[i]){
			int tmp = get(1 , 1,  n , x.fi , i) + x.se;
			cur = min(cur , tmp);
		}
		dp[i] = cur;
		upd(1 , 1,  n, i);	
	}
	if(dp[n] >= 1e15)
		dp[n] = -1;
	ret(dp[n]);
}
                              