#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 100;
struct node {
  int x, y, cost, flag;
  bool operator<(const node& a) const {
    if (x != a.x)
      return x < a.x;
    else
      return flag < a.flag;
  }
};
vector<node> vec;
int sum[200005];
int main() {
  int n, all;
  scanf("%d %d", &n, &all);
  for (int i = 0; i < n; i++) {
    int l, r, cost;
    scanf("%d %d %d", &l, &r, &cost);
    vec.push_back((node){l, r, cost, 0});
    vec.push_back((node){r, l, cost, 1});
  }
  int ans = 2e9 + 100;
  sort(vec.begin(), vec.end());
  for (int i = 0; i < vec.size(); i++) {
    int x = vec[i].x, y = vec[i].y, cost = vec[i].cost, flag = vec[i].flag;
    if (flag == 0) {
      if (all - (y - x + 1) > 0 && sum[all - (y - x + 1)] != 0)
        ans = min(ans, sum[all - (y - x + 1)] + cost);
    } else {
      if (sum[x - y + 1] == 0)
        sum[x - y + 1] = cost;
      else
        sum[x - y + 1] = min(sum[x - y + 1], cost);
    }
  }
  if (ans == 2e9 + 100)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
