#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200005;
const int MAX_DUR = 200005;
int N, X;
int l[MAX_N];
int r[MAX_N];
int cost[MAX_N];
vector<int> vou[MAX_DUR];
int ml[MAX_N];
bool byR(int i, int j) { return r[i] < r[j]; }
bool byL(int i, int j) { return l[i] < l[j]; }
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> X;
  for (int i = 1; i <= N; i++) {
    cin >> l[i] >> r[i] >> cost[i];
    int dur = r[i] - l[i] + 1;
    assert(dur >= 1);
    vou[dur].push_back(i);
  }
  int ans = -1;
  for (int z = 1; z <= X - 1; z++) {
    if (vou[z].size() == 0 || vou[X - z].size() == 0) {
      continue;
    }
    sort(vou[z].begin(), vou[z].end(), byR);
    sort(vou[X - z].begin(), vou[X - z].end(), byL);
    int top = vou[X - z].size();
    assert(top >= 1);
    ml[top - 1] = cost[vou[X - z][top - 1]];
    for (int j = top - 2; j >= 0; j--) {
      ml[j] = min(ml[j + 1], cost[vou[X - z][j]]);
    }
    int j = 0;
    int mr = cost[vou[z][0]];
    for (int i = 0; i < vou[z].size(); i++) {
      int k = vou[z][i];
      mr = min(mr, cost[k]);
      while (j < top && r[k] >= l[vou[X - z][j]]) {
        j++;
      }
      if (j == top) {
        break;
      }
      if (ans == -1 || ans > mr + ml[j]) {
        ans = mr + ml[j];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
