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