#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct data {
  int l, r, cost, type;
} a[2 * N], b[2 * N];
int d[N];
struct cmp {
  bool operator()(data a, data b) {
    int x, y;
    x = (a.type == 1) ? a.l : a.r;
    y = (b.type == 1) ? b.l : b.r;
    if (x != y)
      return x < y;
    else
      return a.type > b.type;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i < N; i++) d[i] = INT_MAX;
  int n, D;
  cin >> n >> D;
  for (int i = 0; i < n; i++) {
    cin >> a[i].l >> a[i].r >> a[i].cost;
    a[n + i] = a[i];
    a[i].type = 1, a[n + i].type = -1;
  }
  sort(a, a + n + n, cmp());
  int x = 0, ans = INT_MAX;
  for (int i = 0; i < n + n; i++) {
    if (a[i].type == -1) {
      int dur = a[i].r - a[i].l + 1;
      d[dur] = min(d[dur], a[i].cost);
      continue;
    }
    int dur = a[i].r - a[i].l + 1;
    int reqd = D - dur;
    if (reqd < 0) continue;
    if (d[reqd] == INT_MAX) continue;
    int cur = a[i].cost + d[reqd];
    if (cur < ans) ans = cur;
  }
  if (ans == INT_MAX) ans = -1;
  cout << ans;
  return 0;
}
