//Hacker, pack your bags!
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector < pair < pair<ll,ll>,ll> > v[2*123456];
vector < pair< pair <ll,ll>,ll> > v2[2*123456];
int main()
{
ll i,j;
ll N,X;
scanf("%lld%lld",&N,&X);
for(i=0;i<N;i++)
{
ll L,R,C;
scanf("%lld%lld%lld",&L,&R,&C);
v[R-L+1].push_back(make_pair(make_pair(L,R),C));
v2[R-L+1].push_back(make_pair(make_pair(L,R),C));
}
for(i=1;i<=X;i++)
{
if((ll)v[i].size()>0)
{
ll temp=1e18;
sort(v[i].begin(),v[i].end());
for(j=v[i].size()-1;j>=0;j--)
{
temp=min(temp,v[i][j].second);
v[i][j].second=temp;
}
}
}
ll ans=1e18;
for(i=1;i<=X;i++)
{
if((ll)v2[i].size()>0 &&(ll)v[X-i].size()>0)
{
for(j=0;j<v2[i].size();j++)
{
ll K=v2[i][j].first.second;
ll L=0;
ll R=v[X-i].size()-1;
ll pos=-1;
if(K>v[X-i][R].first.first)
continue;
while(L<=R)//just greater
{
ll mid=(L+R)/2;
if(v[X-i][mid].first.first>K)
{
pos=mid;
R=mid-1;
}
else
L=mid+1;
}
ans=min(ans,v[X-i][pos].second+v2[i][j].second);
}
}
}
if(ans==1e18)
printf("-1");
else
printf("%lld",ans);
return 0;
}
Ly9IYWNrZXIsIHBhY2sgeW91ciBiYWdzIQojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIGxsIGxvbmcgbG9uZwp2ZWN0b3IgPCBwYWlyIDwgcGFpcjxsbCxsbD4sbGw+ID4gdlsyKjEyMzQ1Nl07CnZlY3RvciA8IHBhaXI8IHBhaXIgPGxsLGxsPixsbD4gPiB2MlsyKjEyMzQ1Nl07CgppbnQgbWFpbigpCnsKCWxsIGksajsKCWxsIE4sWDsKCXNjYW5mKCIlbGxkJWxsZCIsJk4sJlgpOwoJZm9yKGk9MDtpPE47aSsrKQoJewoJCWxsIE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CQkJaWYodltYLWldW21pZF0uZmlyc3QuZmlyc3Q+SykKICAgICAgIAkJCQl7CiAgICAgICAJCQkJCXBvcz1taWQ7CiAgICAgICAJCQkJCVI9bWlkLTE7CiAgICAgICAJCQkJfQogICAgICAgCQkJCWVsc2UKICAgICAgIAkJCQlMPW1pZCsxOwogICAgICAgCQkJfQogICAgICAgCQkJYW5zPW1pbihhbnMsdltYLWldW3Bvc10uc2Vjb25kK3YyW2ldW2pdLnNlY29uZCk7CiAgICAgICAJCX0KICAgICAgIAkKCSAgICAgICB9Cgl9CglpZihhbnM9PTFlMTgpCglwcmludGYoIi0xIik7CgllbHNlCglwcmludGYoIiVsbGQiLGFucyk7CglyZXR1cm4gMDsKfQ==