#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;
}
