#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define mp make_pair
long long n,x;
vector<pair<ll,pair<pair<ll,ll>,ll > > > v;
vector<ll> da;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> x;
bool fo = false;
v.resize(n);
da.resize(n);
for(int i =0;i<n;i++)
{
long long a,b,c;
cin >> a >> b >> c;
v[i] = mp(b-a+1,mp(mp(a,b),c));
}
sort(v.begin(),v.end());
for(int i=0;i<n;i++) da[i] = v[i].first;
ll ans = 1e16;
for(int i=0;i<n;i++)
{
ll d = v[i].first;
if(d == x)
{
ans = min(ans,v[i].second.second); continue;
}
if(d > x)
{
break;
}
vector<ll>::iterator it = lower_bound(da.begin()+i,da.end(),x - d);
ll pos = it - da.begin();
if(*it == x - d)
{
int j = pos;
while(da[j] == x - d && j < n)
{
if(v[i].second.first.second < v[j].second.first.first || v[j].second.first.second < v[i].second.first.first)
{
fo = true;
ans = min(ans,v[i].second.second + v[j].second.second);
}
j++;
}
}
}
//cout << ans << endl;
if(fo)
cout << ans << endl;
else
cout << "-1\n";
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIG1wIG1ha2VfcGFpcgpsb25nIGxvbmcgbix4Owp2ZWN0b3I8cGFpcjxsbCxwYWlyPHBhaXI8bGwsbGw+LGxsID4gPiA+IHY7CnZlY3RvcjxsbD4gZGE7CmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKE5VTEwpOwogICAgY2luID4+IG4gPj4geDsKICAgIGJvb2wgZm8gPSBmYWxzZTsKICAgIHYucmVzaXplKG4pOwogICAgZGEucmVzaXplKG4pOwogICAgZm9yKGludCBpID0wO2k8bjtpKyspCiAgICB7CiAgICAgICAgbG9uZyBsb25nIGEsYixjOwogICAgICAgIGNpbiA+PiBhID4+IGIgPj4gYzsKICAgICAgICB2W2ldID0gbXAoYi1hKzEsbXAobXAoYSxiKSxjKSk7CiAgICB9CiAgICBzb3J0KHYuYmVnaW4oKSx2LmVuZCgpKTsKICAgIGZvcihpbnQgaT0wO2k8bjtpKyspIGRhW2ldID0gdltpXS5maXJzdDsKICAgIGxsIGFucyA9IDFlMTY7CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgewogICAgICAgIGxsIGQgPSB2W2ldLmZpcnN0OwogICAgICAgIGlmKGQgPT0geCkKICAgICAgICB7CiAgICAgICAgICAgIGFucyA9IG1pbihhbnMsdltpXS5zZWNvbmQuc2Vjb25kKTsgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGlmKGQgPiB4KQogICAgICAgIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgICAgIHZlY3RvcjxsbD46Oml0ZXJhdG9yIGl0ID0gbG93ZXJfYm91bmQoZGEuYmVnaW4oKStpLGRhLmVuZCgpLHggLSBkKTsKICAgICAgCiAgICAgICAgbGwgcG9zID0gaXQgLSBkYS5iZWdpbigpOwogICAgICAgCiAgICAgICAgaWYoKml0ID09IHggLSBkKQogICAgICAgIHsKICAgICAgICAgICAKICAgICAgICAgICAgaW50IGogPSBwb3M7CiAgICAgICAgICAgCiAgICAgICAgICAgIHdoaWxlKGRhW2pdID09IHggLSBkICYmIGogPCBuKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZih2W2ldLnNlY29uZC5maXJzdC5zZWNvbmQgPCB2W2pdLnNlY29uZC5maXJzdC5maXJzdCB8fCB2W2pdLnNlY29uZC5maXJzdC5zZWNvbmQgPCB2W2ldLnNlY29uZC5maXJzdC5maXJzdCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGZvID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgICAgICAgYW5zID0gbWluKGFucyx2W2ldLnNlY29uZC5zZWNvbmQgKyB2W2pdLnNlY29uZC5zZWNvbmQpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBqKys7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICAvL2NvdXQgPDwgYW5zIDw8IGVuZGw7CiAgICBpZihmbykKICAgICAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwogICAgZWxzZQogICAgICAgIGNvdXQgPDwgIi0xXG4iOwogICAgcmV0dXJuIDA7Cn0K