#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, z, k, w;
struct node {
  int x, y, cost;
  bool operator<(node const& T) const {
    if (T.x != x)
      return T.x > x;
    else
      return T.y > y;
  }
} A[200005], B[200005];
int val[200005];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = (0); i < (n); i++)
    scanf("%d %d %d", &A[i].x, &A[i].y, &A[i].cost);
  for (int i = (0); i < (n); i++) {
    B[i] = A[i];
    swap(B[i].x, B[i].y);
  }
  sort(A, A + n);
  sort(B, B + n);
  for (int i = (0); i < (n); i++) swap(B[i].x, B[i].y);
  int ans = -1;
  x = 0;
  for (int i = (0); i < (n); i++) {
    while (B[x].y < A[i].x && x < n) {
      k = B[x].y - B[x].x + 1;
      if (val[k] == 0 || val[k] > B[x].cost) val[k] = B[x].cost;
      x++;
    }
    k = A[i].y - A[i].x + 1;
    k = m - k;
    if (k < 0) continue;
    if (val[k] > 0) {
      if (ans == -1 || ans > val[k] + A[i].cost) ans = val[k] + A[i].cost;
    }
  }
  printf("%d\n", ans);
  return 0;
}
