#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  int n, x;
  cin >> n >> x;
  vector<vector<long long> > a(n);
  vector<vector<long long> > b(n);
  map<pair<long long, long long>, long long> cnt;
  for (int i = 0; i < n; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    a[i] = {x, y, z};
    b[i] = {y, x, z};
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) cnt[make_pair(a[i][1] - a[i][0] + 1, a[i][2])]++;
  int j = 0;
  long long ans = 1e12;
  for (int i = 0; i < n; i++) {
    long long cur_r = b[i][0];
    while (j < n && cur_r >= a[j][0]) {
      auto cur = make_pair(a[j][1] - a[j][0] + 1, a[j][2]);
      j++;
      cnt[cur]--;
      if (!cnt[cur]) cnt.erase(cur);
    }
    long long cost = b[i][2];
    long long need_len = x - (b[i][0] - b[i][1] + 1);
    auto nearest_it = (cnt.lower_bound(make_pair(need_len, 0)));
    if (nearest_it == cnt.end()) continue;
    auto nearest = *nearest_it;
    if (nearest.first.first != need_len) continue;
    ans = min(ans, cost + nearest.first.second);
  }
  cout << (ans > 1e10 ? -1 : ans);
  return 0;
}
