#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
typedef vector<int> VI;
#define MAXN 200001
#define pb push_back
#define mp make_pair
#define MOD (ll)1e9+7
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i, a, b) for(int i = a; i < b; ++i)
int n, x;
vector<pair<PII, PII> > a;
int main() {
FASTIO
cin >> n >> x;
rep(i, 0, n) {
int l, r, cost;
cin >> l >> r >> cost;
a.pb(mp(mp(r-l+1, cost), mp(l, r)));
}
sort(a.begin(), a.end());
ll ans = 30000000000000;
int cc = 0;
int i = 0, j = n-1;
for(i = 0; i < n; ) {
int dur1 = a[i].first.first;
int dur2 = a[j].first.first;
ll cost = a[i].first.second + a[j].first.second;
if(i == j)
break;
if(dur1+dur2 > x) {
--j;
}
else if(dur1+dur2 == x) {
if(a[i].second.first > a[j].second.second || a[j].second.first > a[i].second.second)
ans = min(ans, cost);
--j;
}
else
++i;
}
if(ans == 30000000000000)
ans = -1;
cout << ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGxsLCBsbD4gUElJOwp0eXBlZGVmIHZlY3RvcjxpbnQ+IFZJOwoKI2RlZmluZSBNQVhOIDIwMDAwMQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIE1PRCAobGwpMWU5KzcKI2RlZmluZSBGQVNUSU8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiNkZWZpbmUgcmVwKGksIGEsIGIpIGZvcihpbnQgaSA9IGE7IGkgPCBiOyArK2kpCgppbnQgbiwgeDsKdmVjdG9yPHBhaXI8UElJLCBQSUk+ID4gYTsKCmludCBtYWluKCkgewogICAgRkFTVElPCiAgICBjaW4gPj4gbiA+PiB4OwogICAgcmVwKGksIDAsIG4pIHsKICAgICAgICBpbnQgbCwgciwgY29zdDsKICAgICAgICBjaW4gPj4gbCA+PiByID4+IGNvc3Q7CiAgICAgICAgYS5wYihtcChtcChyLWwrMSwgY29zdCksIG1wKGwsIHIpKSk7CiAgICB9CiAgICBzb3J0KGEuYmVnaW4oKSwgYS5lbmQoKSk7CiAgICBsbCBhbnMgPSAzMDAwMDAwMDAwMDAwMDsKICAgIGludCBjYyA9IDA7CiAgICBpbnQgaSA9IDAsIGogPSBuLTE7CiAgICBmb3IoaSA9IDA7IGkgPCBuOyApIHsKICAgICAgICBpbnQgZHVyMSA9IGFbaV0uZmlyc3QuZmlyc3Q7CiAgICAgICAgaW50IGR1cjIgPSBhW2pdLmZpcnN0LmZpcnN0OwogICAgICAgIGxsIGNvc3QgPSBhW2ldLmZpcnN0LnNlY29uZCArIGFbal0uZmlyc3Quc2Vjb25kOwogICAgICAgIGlmKGkgPT0gaikKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgaWYoZHVyMStkdXIyID4geCkgewogICAgICAgICAgICAtLWo7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYoZHVyMStkdXIyID09IHgpIHsKICAgICAgICAgICAgaWYoYVtpXS5zZWNvbmQuZmlyc3QgPiBhW2pdLnNlY29uZC5zZWNvbmQgfHwgYVtqXS5zZWNvbmQuZmlyc3QgPiBhW2ldLnNlY29uZC5zZWNvbmQpCiAgICAgICAgICAgICAgICBhbnMgPSBtaW4oYW5zLCBjb3N0KTsKICAgICAgICAgICAgLS1qOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgICAgICsraTsKICAgIH0KICAgIGlmKGFucyA9PSAzMDAwMDAwMDAwMDAwMCkKICAgICAgICBhbnMgPSAtMTsKICAgIGNvdXQgPDwgYW5zOwp9Cg==