#include <bits/stdc++.h>
using namespace std;
struct X {
  long long st, ed, val, len;
};
X sum[200002];
bool cmp(X a, X b) { return a.ed == b.ed ? a.st < b.st : a.ed < b.ed; }
long long inf = 0x7f7f7f7f7f7f, hua[2][200002], minr, l, r, h, m, n, ji;
int main() {
  while (~scanf("%lld%lld", &n, &m)) {
    minr = inf, memset(hua, inf, sizeof(hua));
    for (int a = 0; a < n; a++)
      scanf("%lld%lld%lld", &sum[a].st, &sum[a].ed, &sum[a].val),
          sum[a].len = sum[a].ed - sum[a].st + 1;
    sort(sum, sum + n, cmp);
    queue<long long> qed, qlen, qval;
    for (int a = 0; a < n; a++) {
      while (!qed.empty()) {
        long long ed = qed.front(), val = qval.front(), len = qlen.front();
        if (ed >= sum[a].st) break;
        if (hua[0][len] > val) hua[0][len] = val, hua[1][len] = ed;
        qed.pop(), qval.pop(), qlen.pop();
      }
      if (hua[1][m - sum[a].len] < sum[a].st && sum[a].len <= m)
        minr = min(minr, sum[a].val + hua[0][m - sum[a].len]);
      if (hua[0][sum[a].len] > sum[a].val)
        qval.push(sum[a].val), qed.push(sum[a].ed), qlen.push(sum[a].len);
    }
    printf("%lld\n", minr == inf ? -1 : minr);
  }
  return 0;
}
