#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 0x7FFFFFFFFFFFFFFF / 2;
unordered_map<long long, vector<pair<long long, long long>>> mpl, mpr;
unordered_map<long long, vector<long long>> mpl2, mpr2;
unordered_map<long long, vector<long long>> mnpl, mnpr;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> ranges;
  vector<long long> costs;
  long long l, r;
  long long c;
  for (int i = 0; i < n; i++) {
    cin >> l >> r >> c;
    ranges.push_back(make_pair(l, r));
    costs.push_back(c);
    mpl[r - l + 1].push_back(make_pair(l, i));
    mpr[r - l + 1].push_back(make_pair(r, i));
  }
  for (int i = 1; i < 200001; i++) {
    if (mpl.count(i)) {
      sort(mpl[i].begin(), mpl[i].end());
      sort(mpr[i].begin(), mpr[i].end());
      long long mn = LLONG_MAX;
      mnpr[i].resize(mpr[i].size());
      mpr2[i].resize(mpr[i].size());
      for (int j = 0; j < mpr[i].size(); j++) {
        mn = min(mn, costs[mpr[i][j].second]);
        mpr2[i][j] = mpr[i][j].first;
        mnpr[i][j] = mn;
      }
      mnpl[i].resize(mpl[i].size());
      mpl2[i].resize(mpl[i].size());
      mn = LLONG_MAX;
      for (int j = mpl[i].size() - 1; j >= 0; j--) {
        mn = min(mn, costs[mpl[i][j].second]);
        mpl2[i][j] = mpl[i][j].first;
        mnpl[i][j] = mn;
      }
    }
  }
  long long ans = LLONG_MAX;
  long long vv[6];
  for (int i = 0; i < ranges.size(); i++) {
    pair<long long, long long> p = ranges[i];
    l = p.first;
    r = p.second;
    long long dur = r - l + 1;
    int rest = m - dur;
    if (dur < m && rest > 0) {
      if (mpl2.count(rest) > 0) {
        auto it1 = lower_bound(mpr2[rest].begin(), mpr2[rest].end(), l);
        if (it1 == mpr2[rest].end()) {
          it1--;
          while (it1 != mpr2[rest].begin() && *it1 >= l) it1--;
          if (*it1 < l) {
            int idx = it1 - mpr2[rest].begin();
            if (costs[i] + mnpr[rest][idx] < ans) {
              ans = costs[i] + mnpr[rest][idx];
            }
          }
        }
        auto it2 = upper_bound(mpl2[rest].begin(), mpl2[rest].end(), r);
        if (it2 != mpl2[rest].end()) {
          int idx = it2 - mpl2[rest].begin();
          if (costs[i] + mnpl[rest][idx] < ans) {
            ans = costs[i] + mnpl[rest][idx];
          }
        }
      }
    }
  }
  if (ans != LLONG_MAX) {
    cout << ans;
  } else {
    cout << "-1";
  }
  return 0;
}
