#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-10;
const int inf = 2000000000;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
  return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
  return n | (1LL << i);
  ;
}
inline long long int resetBit(long long int n, long long int i) {
  return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
  a %= 1000000007;
  (a < 0) && (a += 1000000007);
}
inline long long int modMul(long long int a, long long int b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a * b) % 1000000007;
}
inline long long int modAdd(long long int a, long long int b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a + b) % 1000000007;
}
inline long long int modSub(long long int a, long long int b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline long long int modPow(long long int b, long long int p) {
  long long int r = 1LL;
  while (p) {
    if (p & 1) r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1LL;
  }
  return r;
}
inline long long int modDiv(long long int a, long long int b) {
  return modMul(a, modPow(b, 1000000007 - 2));
}
bool comp(const pair<long long int, pair<long long int, long long int> > &p1,
          const pair<long long int, pair<long long int, long long int> > &p2) {
  return p1.first > p2.first;
}
long long int converter(string a) {
  long long int i, mul = 1, r, t, ans = 0LL;
  if (a.length() == 0) return 0;
  for (i = a.length() - 1; i >= 0; i--) {
    t = a[i] - '0';
    r = t % 10;
    ans += (mul * r);
    mul = mul * 10;
  }
  return ans;
}
pair<pair<long long int, long long int>, long long int> p[200005];
vector<pair<long long int, long long int> > v1[200005];
vector<pair<long long int, long long int> > v2[200005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int n, x, i, ans = (long long int)2e18, id1, id2, cur1, cur2, ans1,
                         ans2, j;
  cin >> n >> x;
  for (i = 1; i <= n; ++i) {
    cin >> p[i].first.first >> p[i].first.second >> p[i].second;
    long long int dif = (p[i].first.second - p[i].first.first + 1);
    v1[dif].push_back(make_pair(p[i].first.second, p[i].second));
    v2[dif].push_back(make_pair(p[i].first.first, p[i].second));
  }
  for (i = 1; i <= 200001; ++i) {
    sort(v1[i].begin(), v1[i].end());
    sort(v2[i].begin(), v2[i].end());
  }
  for (i = 1; i <= 200001; ++i) {
    cur1 = (long long int)2e18;
    for (j = (int)v2[i].size() - 1; j >= 0; --j) {
      cur1 = min(cur1, v2[i][j].second);
      v2[i][j].second = cur1;
    }
    cur1 = (long long int)2e18;
    for (j = 0; j < (int)v1[i].size(); ++j) {
      cur1 = min(cur1, v1[i][j].second);
      v1[i][j].second = cur1;
    }
  }
  for (i = 1; i <= n; ++i) {
    long long int dif = p[i].first.second - p[i].first.first + 1;
    long long int need = x - (dif);
    long long int num1 = p[i].first.first;
    long long int num2 = p[i].first.second;
    cur1 = (long long int)2e18;
    cur2 = p[i].second;
    if (need <= 0) continue;
    if (v1[need].size() > 0 && v2[need].size() > 0) {
      int dis = distance(
          v2[need].begin(),
          lower_bound(v2[need].begin(), v2[need].end(),
                      pair<long long int, long long int>(num2 + 1, -1)));
      if (dis < v2[need].size()) {
        cur1 = v2[need][dis].second;
      }
      int dis2 = distance(
          v1[need].begin(),
          lower_bound(v1[need].begin(), v1[need].end(),
                      pair<long long int, long long int>(num1 - 1, -1)));
      if (dis2 < v1[need].size()) {
        ;
        if (v1[need][dis2].first >= num1) dis2--;
        if (dis2 >= 0) cur1 = min(cur1, v1[need][dis2].second);
      }
    }
    if (v1[need].size() == 0 && v2[need].size() > 0) {
      int dis = distance(
          v2[need].begin(),
          lower_bound(v2[need].begin(), v2[need].end(),
                      pair<long long int, long long int>(num2 + 1, -1)));
      if (dis < v2[need].size()) {
        cur1 = v2[need][dis].second;
      }
    }
    if (v1[need].size() > 0 && v2[need].size() == 0) {
      int dis2 = distance(
          v1[need].begin(),
          lower_bound(v1[need].begin(), v1[need].end(),
                      pair<long long int, long long int>(num1 - 1, -1)));
      if (dis2 < v1[need].size()) {
        if (v1[need][dis2].first >= num1) dis2--;
        if (dis2 >= 0) cur1 = min(cur1, v1[need][dis2].second);
      }
    }
    if (cur1 != (long long int)2e18) {
      ;
      ans = min(cur1 + cur2, ans);
    }
  }
  if (ans == (long long int)2e18) {
    cout << -1;
    return 0;
  }
  cout << ans << '\n';
  return 0;
}
