#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
using namespace std;
int n, m, L[200010], R[200010], Min[200010];
struct node {
  int l;
  int r;
  int num;
  int v;
} p[200010];
bool cmp(node a, node b) {
  if (a.num == b.num) return a.l < b.l;
  return a.num < b.num;
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(L, 0, sizeof(L));
    memset(R, 0, sizeof(R));
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &p[i].l, &p[i].r, &p[i].v);
      p[i].num = p[i].r - p[i].l + 1;
    }
    sort(p + 1, p + 1 + n, cmp);
    for (int i = 1; i <= n; i++) {
      int x = p[i].num;
      if (L[x] == 0) {
        L[x] = i;
        R[x] = i;
      } else
        R[x] = i;
    }
    Min[n] = p[n].v;
    for (int i = n - 1; i > 0; i--) {
      if (p[i].num == p[i + 1].num) {
        Min[i] = min(Min[i + 1], p[i].v);
      } else
        Min[i] = p[i].v;
    }
    int ans = -1;
    int ll, rr;
    for (int i = 1; i <= n; i++) {
      int k = m - p[i].num;
      if (k <= 0) break;
      if (!L[k]) continue;
      ll = L[k];
      rr = R[k];
      while (ll <= rr) {
        int z = ll + rr >> 1;
        if (p[i].r >= p[z].l)
          ll = z + 1;
        else
          rr = z - 1;
      }
      if (ll > R[k]) continue;
      if (ans == -1)
        ans = Min[ll] + p[i].v;
      else
        ans = min(Min[ll] + p[i].v, ans);
    }
    printf("%d\n", ans == inf ? -1 : ans);
  }
  return 0;
}
