#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ll n,k,u,v,w,days;
cin>>n>>k;
vector<pair<ll,pair<ll,ll> > > deal;//(w,(u,v))
vector<pair<ll,ll> > pack[k];
vector<ll> pack1[k],pack2[k];
for(ll i=0;i<n;i++)
{
cin>>u>>v>>w;
days=v-u+1;
if(days>=k)
continue;
deal.push_back({w,{u,v}});
pack[days].push_back({u,w});
}
for(ll i=0;i<k;i++)
sort(pack[i].begin(),pack[i].end());
for(ll i=0;i<k;i++)
{
for(ll j=0;j<pack[i].size();j++)
{
pack1[i].push_back(pack[i][j].first);
pack2[i].push_back(pack[i][j].second);
}
ll mini=1e18;
if(pack2[i].size()==0)
continue;
for(ll j=pack2[i].size()-1;j>=0;j--)
{
mini=min(mini,pack2[i][j]);
pack2[i][j]=mini;
}
}
ll ans=1e18;
for(ll i=0;i<deal.size();i++)
{
ll cost=deal[i].first;
u=deal[i].second.first;
v=deal[i].second.second;
days=v-u+1;
ll left=k-days;
days=left;
//search in pack1[days]
v++;
ll pos=lower_bound(pack1[days].begin(),pack1[days].end(),v)-pack1[days].begin();
if(pos==pack1[days].size())
continue;
cost+=pack2[days][pos];
ans=min(ans,cost);
}
if(ans==1e18)
cout<<-1;
else
cout<<ans;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcgaW50CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBtYWluKCkKewogICAgbGwgbixrLHUsdix3LGRheXM7CiAgICBjaW4+Pm4+Pms7CiAgICB2ZWN0b3I8cGFpcjxsbCxwYWlyPGxsLGxsPiA+ID4gZGVhbDsvLyh3LCh1LHYpKQogICAgdmVjdG9yPHBhaXI8bGwsbGw+ID4gcGFja1trXTsKICAgIHZlY3RvcjxsbD4gcGFjazFba10scGFjazJba107CiAgICBmb3IobGwgaT0wO2k8bjtpKyspCiAgICB7CiAgICAgICAgY2luPj51Pj52Pj53OwogICAgICAgIGRheXM9di11KzE7CiAgICAgICAgaWYoZGF5cz49aykKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgZGVhbC5wdXNoX2JhY2soe3cse3Usdn19KTsKICAgICAgICBwYWNrW2RheXNdLnB1c2hfYmFjayh7dSx3fSk7CiAgICB9CiAgICBmb3IobGwgaT0wO2k8aztpKyspCiAgICAgICAgc29ydChwYWNrW2ldLmJlZ2luKCkscGFja1tpXS5lbmQoKSk7CiAgICBmb3IobGwgaT0wO2k8aztpKyspCiAgICB7CiAgICAgICAgZm9yKGxsIGo9MDtqPHBhY2tbaV0uc2l6ZSgpO2orKykKICAgICAgICB7CiAgICAgICAgICAgIHBhY2sxW2ldLnB1c2hfYmFjayhwYWNrW2ldW2pdLmZpcnN0KTsKICAgICAgICAgICAgcGFjazJbaV0ucHVzaF9iYWNrKHBhY2tbaV1bal0uc2Vjb25kKTsKICAgICAgICB9CiAgICAgICAgbGwgbWluaT0xZTE4OwogICAgICAgIGlmKHBhY2syW2ldLnNpemUoKT09MCkKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgZm9yKGxsIGo9cGFjazJbaV0uc2l6ZSgpLTE7aj49MDtqLS0pCiAgICAgICAgewogICAgICAgICAgICBtaW5pPW1pbihtaW5pLHBhY2syW2ldW2pdKTsKICAgICAgICAgICAgcGFjazJbaV1bal09bWluaTsKICAgICAgICB9CiAgICB9CiAgICBsbCBhbnM9MWUxODsKICAgIGZvcihsbCBpPTA7aTxkZWFsLnNpemUoKTtpKyspCiAgICB7CiAgICAgICAgbGwgY29zdD1kZWFsW2ldLmZpcnN0OwogICAgICAgIHU9ZGVhbFtpXS5zZWNvbmQuZmlyc3Q7CiAgICAgICAgdj1kZWFsW2ldLnNlY29uZC5zZWNvbmQ7CiAgICAgICAgZGF5cz12LXUrMTsKICAgICAgICBsbCBsZWZ0PWstZGF5czsKICAgICAgICBkYXlzPWxlZnQ7CiAgICAgICAgLy9zZWFyY2ggaW4gcGFjazFbZGF5c10KICAgICAgICB2Kys7CiAgICAgICAgbGwgcG9zPWxvd2VyX2JvdW5kKHBhY2sxW2RheXNdLmJlZ2luKCkscGFjazFbZGF5c10uZW5kKCksdiktcGFjazFbZGF5c10uYmVnaW4oKTsKICAgICAgICBpZihwb3M9PXBhY2sxW2RheXNdLnNpemUoKSkKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgY29zdCs9cGFjazJbZGF5c11bcG9zXTsKICAgICAgICBhbnM9bWluKGFucyxjb3N0KTsKICAgIH0KICAgIGlmKGFucz09MWUxOCkKICAgICAgICBjb3V0PDwtMTsKICAgIGVsc2UKICAgICAgICBjb3V0PDxhbnM7Cn0=