#include <bits/stdc++.h>
using namespace std;
struct eventt {
  int X;
  long long c;
  int isSt;
  int sing;
  int d;
  eventt(int X2, long long c2, int isSt2, int sing2, int d2) {
    isSt = isSt2;
    X = X2;
    c = c2;
    d = d2;
    sing = sing2;
  }
};
int cmp(eventt a, eventt b) {
  if (a.X == b.X) return a.isSt > b.isSt;
  return a.X < b.X;
}
long long csts[224345];
long long cs[214345];
int main() {
  int n, x;
  cin >> n >> x;
  int l, r;
  long long c;
  int d;
  vector<eventt> v;
  long long currMin = 1e12;
  for (int i = 0; i <= 200000; i++) csts[i] = cs[i] = currMin;
  for (int i = 0; i < n; i++) {
    cin >> l >> r >> c;
    d = (r - l + 1);
    if (d >= x) continue;
    eventt* e = new eventt(l, c, 1, l == r, d);
    eventt* e2 = new eventt(r, c, 0, l == r, d);
    v.push_back(*e);
    v.push_back(*e2);
    if (d == 1) cs[l] = min(c, cs[l]);
  }
  sort(v.begin(), v.end(), cmp);
  for (int i = 0; i < v.size(); i++) {
    if (v[i].isSt) {
      currMin = min(currMin, csts[x - v[i].d] + v[i].c);
    } else {
      csts[v[i].d] = min(csts[v[i].d], v[i].c);
    }
  }
  if (x == 2) {
    sort(cs, cs + 200001);
    currMin = cs[0] + cs[1];
  }
  if (currMin >= 1e12) currMin = -1;
  cout << currMin << endl;
}
