#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define sz(a) a.size()
#define endl '\n'
#define inf int(1e10)
#define mod inf+7
#define fast_io() std::ios::sync_with_stdio(0)
#define rep(i,a,b) for( int i=a; i<b; i++)
using namespace std;
struct voucher{
int l;
int r;
int cost;
int dur;
};
bool comp(voucher a, voucher b)
{
if ( (a.dur)<(b.dur))
return 1;
else if (a.dur==b.dur)
return a.cost<=b.cost;
else
return 0;
}
int main()
{
int n,x;
voucher t[200005];//TO CHANGE LATER
cin>>n>>x;
for(int i=0;i<n;i++)
{
cin>>t[i].l>>t[i].r>>t[i].cost;
t[i].dur=t[i].r-t[i].l+1;
}
sort(t,t+n,comp);
int i=0; int j=n-1;
int ans=inf;
while(i<j)
{
if (t[i].dur+t[j].dur==x)
{
if (t[i].r<t[j].l || t[j].r<t[i].l)
ans=min(ans,t[i].cost+t[j].cost);
i--;
}
if (t[i].dur+t[j].dur>x)
j--;
if (t[i].dur+t[j].dur<x)
i++;
}
if (ans==inf )
cout<<-1;
else
cout<<ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIHN6KGEpIGEuc2l6ZSgpCiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgaW5mIGludCgxZTEwKQojZGVmaW5lIG1vZCBpbmYrNwojZGVmaW5lIGZhc3RfaW8oKSBzdGQ6Omlvczo6c3luY193aXRoX3N0ZGlvKDApCiNkZWZpbmUgcmVwKGksYSxiKSBmb3IoIGludCBpPWE7IGk8YjsgaSsrKQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiBzdHJ1Y3Qgdm91Y2hlcnsKICBpbnQgbDsKICBpbnQgcjsKICBpbnQgY29zdDsKICBpbnQgZHVyOwogfTsKCmJvb2wgY29tcCh2b3VjaGVyIGEsIHZvdWNoZXIgYikKICB7CiAgICBpZiAoIChhLmR1cik8KGIuZHVyKSkKICAgICAgcmV0dXJuIDE7CiAgICBlbHNlIGlmIChhLmR1cj09Yi5kdXIpCiAgICAgIHJldHVybiBhLmNvc3Q8PWIuY29zdDsKICAgIGVsc2UKICAgICAgcmV0dXJuIDA7CiAgfQogIGludCBtYWluKCkKICAgewogICAgaW50IG4seDsKICAgIHZvdWNoZXIgdFsyMDAwMDVdOy8vVE8gQ0hBTkdFIExBVEVSCiAgICBjaW4+Pm4+Png7CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICB7CiAgICAgICAgY2luPj50W2ldLmw+PnRbaV0ucj4+dFtpXS5jb3N0OwogICAgICAgIHRbaV0uZHVyPXRbaV0uci10W2ldLmwrMTsKICAgICAgfQoKICAgc29ydCh0LHQrbixjb21wKTsKCiAgIGludCBpPTA7IGludCBqPW4tMTsKICAgaW50IGFucz1pbmY7CiAgIHdoaWxlKGk8aikKICAgewogICAgIGlmICh0W2ldLmR1cit0W2pdLmR1cj09eCkKICAgICB7CiAgICAgICBpZiAodFtpXS5yPHRbal0ubCB8fCB0W2pdLnI8dFtpXS5sKQogICAgICAgICBhbnM9bWluKGFucyx0W2ldLmNvc3QrdFtqXS5jb3N0KTsKCiAgICAgICAgaS0tOwogICAgIH0KICAgICAgaWYgKHRbaV0uZHVyK3Rbal0uZHVyPngpCiAgICAgICAgai0tOwogICAgICBpZiAodFtpXS5kdXIrdFtqXS5kdXI8eCkKICAgICAgICBpKys7CiAgIH0KCiAgIGlmIChhbnM9PWluZiApCiAgICBjb3V0PDwtMTsKICAgZWxzZQogICAgY291dDw8YW5zOwogICB9Cg==