#include <bits/stdc++.h>
using namespace std;
const long long inf = 2e9 + 10;
const int maxn = 2e5 + 10;
vector<pair<pair<int, int>, pair<int, int> > > vec;
int bestCost[maxn];
int main() {
  std::ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int l, r, cost;
    cin >> l >> r >> cost;
    vec.push_back(make_pair(make_pair(l, -1), make_pair(r, cost)));
    vec.push_back(make_pair(make_pair(r, 1), make_pair(l, cost)));
  }
  sort(vec.begin(), vec.end());
  int sz = vec.size();
  fill(bestCost, bestCost + maxn, inf);
  long long ans = 2LL * inf;
  for (int i = 0; i < sz; i++) {
    int type = vec[i].first.second, a = vec[i].first.first,
        b = vec[i].second.first;
    int cost = vec[i].second.second;
    if (type == -1) {
      int x = m - (b - a + 1);
      if (x > 0) ans = min(ans, 1LL * cost + 1LL * bestCost[x]);
    } else {
      bestCost[a - b + 1] = min(bestCost[a - b + 1], cost);
    }
  }
  cout << (ans >= inf ? -1 : ans) << endl;
  return 0;
}
