#include <bits/stdc++.h>
using namespace std;
const int maxn = 200001;
int l[maxn], r[maxn], cost[maxn], bcost[maxn];
int n, x;
const int inf = 2e9 + 2;
long long ans = 2LL * inf;
long long minc(long long a, long long b) { return a < b ? a : b; }
vector<pair<pair<int, int>, pair<int, int> > > a;
void solve() {
  sort((a).begin(), (a).end());
  for (long long i = 0; i < (long long)maxn; ++i) bcost[i] = inf;
  for (long long i = 0; i < (long long)a.size(); ++i) {
    int type = a[i].first.second;
    if (!type) {
      int dur = a[i].second.first - a[i].first.first + 1;
      if (dur <= x)
        ans = minc(ans, 1LL * a[i].second.second + 1LL * bcost[x - dur]);
    } else {
      int dur = a[i].first.first - a[i].second.first + 1;
      bcost[dur] = minc(bcost[dur], a[i].second.second);
    }
  }
  if (ans >= inf)
    puts("-1");
  else
    cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> x;
  a.clear();
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i] >> cost[i];
    a.push_back(make_pair(make_pair(l[i], 0), make_pair(r[i], cost[i])));
    a.push_back(make_pair(make_pair(r[i], 1), make_pair(l[i], cost[i])));
  }
  solve();
  return 0;
}
