#include <bits/stdc++.h>
using namespace std;
const int INF_I = 1e9;
const long long INF_LL = 1e18;
const int MOD = 1000000007;
const double eps = 1e-6;
struct s {
  int l;
  int r;
  int c;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<s> a(n);
  vector<pair<vector<pair<int, int>>, vector<pair<int, int>>>> b(k);
  for (int i = 0; i < n; i++) {
    cin >> a[i].l >> a[i].r >> a[i].c;
    if (a[i].r - a[i].l + 1 < k) {
      b[a[i].r - a[i].l + 1].first.emplace_back(make_pair(a[i].l, a[i].c));
      b[a[i].r - a[i].l + 1].second.emplace_back(make_pair(a[i].r, a[i].c));
    }
  }
  for (int i = 0; i < k; i++) {
    sort(begin(b[i].first), end(b[i].first));
    sort(begin(b[i].second), end(b[i].second));
    for (int j = 1; j < (int)b[i].second.size(); j++)
      b[i].second[j].second =
          min(b[i].second[j - 1].second, b[i].second[j].second);
    for (int j = (int)b[i].first.size() - 2; j >= 0; j--)
      b[i].first[j].second =
          min(b[i].first[j + 1].second, b[i].first[j].second);
  }
  long long mn = INF_LL;
  for (int i = 0; i < n; i++) {
    int need = k - (a[i].r - a[i].l + 1);
    if (need <= 0 || (int)b[need].first.size() == 0) continue;
    int l = upper_bound(begin(b[need].first), end(b[need].first),
                        make_pair(a[i].r, INF_I + 1)) -
            b[need].first.begin();
    if (l < (int)b[need].first.size())
      mn = min(mn, (long long)a[i].c + b[need].first[l].second);
    int r = lower_bound(begin(b[need].second), end(b[need].second),
                        make_pair(a[i].l, -1)) -
            b[need].second.begin() - 1;
    if (r >= 0) mn = min(mn, (long long)a[i].c + b[need].second[r].second);
  }
  if (mn == INF_LL)
    cout << "-1\n";
  else
    cout << mn << "\n";
  return 0;
}
