#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
long long n, x, ret[N], ans = 0x7fffffff;
struct nd {
  long long w, l, r;
} a[N];
bool cmp(nd a, nd b) { return a.l < b.l; }
bool operator<(nd a, nd b) { return a.r > b.r; }
int main() {
  cin >> n >> x;
  memset(ret, 0x3f, sizeof(ret));
  for (int i = 1; i <= n; ++i) cin >> a[i].l >> a[i].r >> a[i].w;
  sort(a + 1, a + n + 1, cmp);
  priority_queue<nd> q;
  for (int i = 1; i <= n; ++i) {
    while (!q.empty() && q.top().r < a[i].l) {
      nd Q = q.top();
      q.pop();
      ret[Q.r - Q.l + 1] = min(ret[Q.r - Q.l + 1], Q.w);
    }
    int k = x - (a[i].r - a[i].l + 1);
    if (ret[k] == 2e9 + 1 || k < 0) continue;
    ans = min(ans, a[i].w + ret[k]);
    q.push(a[i]);
  }
  cout << (ans == 0x7fffffff ? -1 : ans);
}
