#include <bits/stdc++.h>
using namespace std;
struct node {
  int start;
  int finish;
  long long int cost;
};
vector<int> start[200000];
vector<int> finish[200000];
int main() {
  int n;
  long long int duration;
  cin >> n >> duration;
  node act[n];
  for (int i = 0; i < n; i++) {
    cin >> act[i].start >> act[i].finish >> act[i].cost;
    start[act[i].start].push_back(i);
    finish[act[i].finish].push_back(i);
  }
  map<int, long long int> mp;
  long long int ans = INT_MAX;
  for (int i = 1; i <= 200000; i++) {
    for (int j = 0; j < start[i].size(); j++) {
      node mynode = act[start[i][j]];
      if (mp.count(duration - mynode.finish + mynode.start - 1) != 0) {
        ans = min(
            ans, mp[duration - mynode.finish + mynode.start - 1] + mynode.cost);
      }
    }
    for (int j = 0; j < finish[i].size(); j++) {
      node mynode = act[finish[i][j]];
      if (mp.count(mynode.finish - mynode.start + 1) != 0) {
        mp[mynode.finish - mynode.start + 1] =
            min(mp[mynode.finish - mynode.start + 1], mynode.cost);
      } else
        mp[mynode.finish - mynode.start + 1] = mynode.cost;
    }
  }
  if (ans == INT_MAX)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
