#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")
using namespace std;
const long long hell = 1000000000007;
vector<long long> t[200010];
map<long long, vector<pair<long long, long long> > > m;
void build(long long node, long long pos, long long start, long long end) {
  if (start == end) {
    t[node][pos] = m[node][start].second;
    return;
  }
  long long mid = start + end;
  mid /= 2;
  build(node, 2 * pos, start, mid);
  build(node, 2 * pos + 1, mid + 1, end);
  t[node][pos] = min(t[node][2 * pos], t[node][2 * pos + 1]);
}
long long query(long long node, long long pos, long long start, long long end,
                long long l, long long r) {
  if (start > r || end < l || l > r) return hell;
  if (start >= l && end <= r) return t[node][pos];
  long long mid = start + end;
  mid /= 2;
  return min(query(node, 2 * pos, start, mid, l, r),
             query(node, 2 * pos + 1, mid + 1, end, l, r));
}
long long solve() {
  long long n, k;
  cin >> n >> k;
  vector<pair<pair<long long, long long>, long long> > v;
  for (long long i = 0; i < n; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    v.push_back({{x, y}, z});
  }
  sort((v).begin(), (v).end());
  for (long long i = 0; i < n; i++) {
    long long x = v[i].first.first, y = v[i].first.second, z = v[i].second;
    m[y - x + 1].push_back({x, z});
  }
  for (auto &x : m) {
    sort((m[x.first]).begin(), (m[x.first]).end());
    long long len = x.second.size();
    t[x.first].resize(4 * len + 10);
    build(x.first, 1, 0, len - 1);
  }
  long long ans = hell;
  for (long long i = 0; i < n; i++) {
    long long x = v[i].first.first, y = v[i].first.second, z = v[i].second;
    long long len = m[k - (y - x + 1)].size();
    pair<long long, long long> p;
    p.first = y;
    p.second = 2 * hell;
    long long l = lower_bound((m[k - (y - x + 1)]).begin(),
                              (m[k - (y - x + 1)]).end(), p) -
                  m[k - (y - x + 1)].begin();
    ans = min(ans, z + query(k - (y - x + 1), 1, 0, len - 1, l, len - 1));
  }
  if (ans == hell)
    cout << -1;
  else
    cout << ans;
  return 0;
}
signed main() {
  long long t = 1;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin.exceptions(cin.failbit);
  clock_t time_req;
  long long k = 1;
  while (t--) {
    solve();
  }
  time_req = clock() - time_req;
  return 0;
}
