#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, x;
  cin >> n >> x;
  vector<tuple<int, int, int>> v(n);
  map<int, multiset<int>> idx;
  for (int i = 0; i < n; ++i) {
    cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]);
    int len = get<1>(v[i]) - get<0>(v[i]) + 1;
    if (idx.find(len) == idx.end()) {
      idx[len] = multiset<int>();
    }
    auto& s = idx[len];
    s.insert(get<2>(v[i]));
  }
  sort(v.begin(), v.end(),
       [](const tuple<int, int, int>& a, const tuple<int, int, int>& b) {
         return (get<1>(a) < get<1>(b));
       });
  vector<tuple<int, int, int>> vv(v);
  sort(vv.begin(), vv.end());
  auto it = vv.begin();
  int ans = numeric_limits<int>::max();
  for (const auto& e : v) {
    while (it != vv.end() && get<0>(*it) <= get<1>(e)) {
      int l = get<1>(*it) - get<0>(*it) + 1;
      auto& ss = idx[l];
      auto r = ss.find(get<2>(*it));
      idx[get<1>(*it) - get<0>(*it) + 1].erase(r);
      it++;
    }
    int len = x - (get<1>(e) - get<0>(e) + 1);
    if (idx.find(len) == idx.end()) continue;
    auto& s = idx[len];
    if (s.empty()) continue;
    ans = min(ans, get<2>(e) + (*s.begin()));
  }
  if (ans == numeric_limits<int>::max()) ans = -1;
  cout << ans << endl;
  return 0;
}
