#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
vector<pair<int, int>> rightEndPoint[N];
int main() {
  int n, X;
  scanf("%d%d", &n, &X);
  for (int idx = 0; idx < n; idx++) {
    int L, R, val;
    scanf("%d%d%d", &L, &R, &val);
    rightEndPoint[R - L + 1].push_back({R, val});
  }
  for (int len = 1; len <= X; len++) {
    sort(rightEndPoint[len].begin(), rightEndPoint[len].end());
  }
  long long ret = (1LL << 60);
  for (int len = 1; len < X; len++) {
    int anotherLen = X - len;
    int idx = 0;
    long long addval = (1LL << 60);
    for (pair<int, int> val : rightEndPoint[len]) {
      while (idx < rightEndPoint[anotherLen].size() &&
             rightEndPoint[anotherLen][idx].first <= val.first - len) {
        addval = min(addval, (long long)rightEndPoint[anotherLen][idx].second);
        idx++;
      }
      ret = min(ret, addval + val.second);
    }
  }
  if (ret >= (1LL << 60)) ret = -1;
  printf("%d\n", ret);
  return 0;
}
