#include <bits/stdc++.h>
using namespace std;
int n, x;
struct vouch {
  int l, r, c, idx;
};
vouch v1[200010], v2[200010];
pair<int, int> sc[200010];
int ids[200010];
bool srt1(const vouch& a, const vouch& b) { return a.l > b.l; }
bool srt2(const vouch& a, const vouch& b) { return a.r > b.r; }
int main() {
  cin >> n >> x;
  vouch tmp;
  for (int i = 0; i < n; ++i) {
    cin >> tmp.l >> tmp.r >> tmp.c;
    tmp.idx = i;
    v1[i] = v2[i] = tmp;
  }
  sort(v1, v1 + n, srt1);
  sort(v2, v2 + n, srt2);
  for (int i = 0; i < 200010; ++i) sc[i].first = sc[i].second = 0;
  int j(0);
  long long ans(1e18);
  for (int i = 0; i < n; ++i) {
    while (j < n && v2[i].r < v1[j].l) {
      int idx(v1[j].r - v1[j].l + 1);
      if (!sc[idx].first) {
        sc[idx].first = v1[j].c;
        ids[idx] = v1[j].idx;
      } else {
        if (v1[j].c < sc[idx].first) {
          ids[idx] = v1[j].idx;
          sc[idx].second = sc[idx].first;
          sc[idx].first = v1[j].c;
        } else
          sc[idx].second = min(sc[idx].second, v1[j].c);
      }
      ++j;
    }
    int id(v2[i].r - v2[i].l + 1);
    if (id >= x) continue;
    if (v2[i].idx != ids[x - id] && sc[x - id].first &&
        sc[x - id].first + v2[i].c < ans) {
      ans = v2[i].c + sc[x - id].first;
    } else if (v2[i].idx == ids[x - id] && sc[x - id].second &&
               sc[x - id].second + v2[i].c < ans) {
      ans = v2[i].c + sc[x - id].second;
    }
  }
  if (ans > 1e17)
    cout << -1;
  else
    cout << ans;
  return 0;
}
