#include <bits/stdc++.h>
using namespace std;
int n, x;
vector<int> starts[200002];
vector<int> cost[200002];
struct vac {
  int st, sf, cost;
};
vac v[200002];
bool test(vac a, vac b) { return a.st < b.st; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> x;
  for (int i = 1; i <= n; ++i) cin >> v[i].st >> v[i].sf >> v[i].cost;
  sort(v + 1, v + n + 1, test);
  for (int i = 1; i <= n; ++i) {
    starts[v[i].sf - v[i].st + 1].push_back(v[i].st);
    cost[v[i].sf - v[i].st + 1].push_back(v[i].cost);
  }
  for (int i = 1; i <= 200000; ++i)
    for (int j = cost[i].size() - 2; j >= 0; --j)
      cost[i][j] = min(cost[i][j], cost[i][j + 1]);
  int sol = 2e9 + 1;
  for (int i = 1; i <= n; ++i) {
    int len = v[i].sf - v[i].st + 1;
    int C = v[i].cost;
    if (len >= x) continue;
    int b = 0;
    int e = starts[x - len].size() - 1;
    while (b <= e) {
      int mid = (b + e) / 2;
      if (starts[x - len][mid] > v[i].sf &&
          (mid == 0 || starts[x - len][mid - 1] <= v[i].sf)) {
        C += cost[x - len][mid];
        sol = min(sol, C);
        break;
      }
      if (starts[x - len][mid] > v[i].sf)
        e = mid - 1;
      else
        b = mid + 1;
    }
  }
  if (sol == 2e9 + 1)
    cout << -1;
  else
    cout << sol;
  return 0;
}
