#include <bits/stdc++.h>
using namespace std;
struct str {
  int l, r, cost, diff;
};
bool comp(str a, str b) {
  if (a.l == b.l) {
    if (a.r == b.r)
      return a.cost < b.cost;
    else
      return a.r < b.r;
  }
  return a.l < b.l;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<str> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].l >> a[i].r >> a[i].cost;
    a[i].diff = a[i].r - a[i].l + 1;
  }
  sort(a.begin(), a.end(), comp);
  vector<vector<int> > di(200005);
  for (int i = 0; i < n; i++) {
    di[a[i].diff].push_back(i);
  }
  vector<vector<int> > cos(200005);
  for (int i = 0; i < 200005; i++) {
    cos[i] = di[i];
    for (int j = di[i].size() - 1; j >= 0; j--) {
      if (j == di[i].size() - 1)
        cos[i][j] = a[di[i][j]].cost;
      else
        cos[i][j] = min(a[di[i][j]].cost, cos[i][j + 1]);
    }
  }
  int ans = 2147483647;
  for (int i = 0; i < n; i++) {
    int x = a[i].diff, temp = a[i].cost;
    int k = m - x;
    if (k <= 0 || di[k].size() == 0) continue;
    int l = 0, r = di[k].size() - 1, mid;
    int an = 2147483647;
    while (l <= r) {
      mid = (l + r) / 2;
      if (a[di[k][mid]].l > a[i].r) {
        an = min(an, mid);
        r = mid - 1;
      } else {
        l = mid + 1;
      }
    }
    if (an == 2147483647) continue;
    ans = min(ans, temp + cos[k][an]);
  }
  if (ans == 2147483647)
    cout << -1;
  else
    cout << ans;
}
