#include <bits/stdc++.h>
using namespace std;
pair<int, pair<int, int> > lefter[200001];
pair<int, pair<int, int> > righter[200001];
int cl[4 * 200001], cr[4 * 200001];
void intialize(int node, int st, int en) {
  if (st == en) {
    cl[node] = lefter[st].second.second;
    cr[node] = righter[st].second.second;
    return;
  }
  int mid = (st + en) / 2;
  intialize(2 * node, st, mid);
  intialize(2 * node + 1, mid + 1, en);
  cl[node] = min(cl[2 * node], cl[2 * node + 1]);
  cr[node] = min(cr[2 * node], cr[2 * node + 1]);
}
int queryl(int node, int st, int en, int l, int r) {
  if (en < l || st > r) return 1000000001;
  if (st >= l && en <= r) return cl[node];
  int mid = (st + en) / 2;
  return min(queryl(2 * node, st, mid, l, r),
             queryl(2 * node + 1, mid + 1, en, l, r));
}
int queryr(int node, int st, int en, int l, int r) {
  if (en < l || st > r) return 1000000001;
  if (st >= l && en <= r) return cr[node];
  int mid = (st + en) / 2;
  return min(queryr(2 * node, st, mid, l, r),
             queryr(2 * node + 1, mid + 1, en, l, r));
}
pair<int, int> getRange(int rest, int n) {
  int L = 0, R = n - 1, low = -1, high = -1;
  while (L <= R) {
    int mid = (L + R) / 2;
    if (lefter[mid].first == rest) {
      low = mid;
      R = mid - 1;
    } else if (lefter[mid].first > rest)
      R = mid - 1;
    else
      L = mid + 1;
  }
  L = 0, R = n - 1;
  while (L <= R) {
    int mid = (L + R) / 2;
    if (lefter[mid].first == rest) {
      high = mid;
      L = mid + 1;
    } else if (lefter[mid].first > rest)
      R = mid - 1;
    else
      L = mid + 1;
  }
  return make_pair(low, high);
}
int binl(int st, int en, int ret) {
  int L = st, R = en, x = -1;
  while (L <= R) {
    int mid = (L + R) / 2;
    if (lefter[mid].second.first > ret) {
      x = mid;
      R = mid - 1;
    } else
      L = mid + 1;
  }
  return x;
}
int binr(int st, int en, int lev) {
  int L = st, R = en, x = -1;
  while (L <= R) {
    int mid = (L + R) / 2;
    if (righter[mid].second.first < lev) {
      x = mid;
      L = mid + 1;
    } else
      R = mid - 1;
  }
  return x;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, x;
  cin >> n >> x;
  int l, r, c;
  for (int i = 0; i < n; i++) {
    cin >> l >> r >> c;
    lefter[i] = make_pair(r - l + 1, make_pair(l, c));
    righter[i] = make_pair(r - l + 1, make_pair(r, c));
  }
  sort(lefter, lefter + n);
  sort(righter, righter + n);
  intialize(1, 0, n - 1);
  long long mini = 2LL * (long long)1000000001;
  for (int i = 0; i < n; i++) {
    if (lefter[i].first < x) {
      pair<int, int> temp = getRange(x - lefter[i].first, n);
      if (temp.first != -1 && temp.second != -1) {
        pair<int, int> tel =
            make_pair(binl(temp.first, temp.second,
                           lefter[i].first + lefter[i].second.first - 1),
                      temp.second);
        if (tel.first != -1 && tel.second != -1) {
          if (tel.first == tel.second) {
            if (i != tel.first)
              mini = min(mini, (long long)lefter[i].second.second +
                                   (long long)lefter[tel.first].second.second);
          } else {
            int shit = 1000000001;
            if (i == tel.first)
              shit = min(shit, queryl(1, 0, n - 1, tel.first + 1, tel.second));
            else if (i == tel.second)
              shit = min(shit, queryl(1, 0, n - 1, tel.first, tel.second - 1));
            else if (i > tel.first && i < tel.second)
              shit = min(shit, min(queryl(1, 0, n - 1, tel.first, i - 1),
                                   queryl(1, 0, n - 1, i + 1, tel.second)));
            else
              shit = min(shit, queryl(1, 0, n - 1, tel.first, tel.second));
            mini =
                min(mini, (long long)lefter[i].second.second + (long long)shit);
          }
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (lefter[i].first < x) {
      pair<int, int> temp = getRange(x - lefter[i].first, n);
      if (temp.first != -1 && temp.second != -1) {
        pair<int, int> ter = make_pair(
            temp.first, binr(temp.first, temp.second,
                             righter[i].second.first - righter[i].first + 1));
        if (ter.first != -1 && ter.second != -1) {
          if (ter.first == ter.second) {
            if (i != ter.first)
              mini = min(mini, (long long)righter[i].second.second +
                                   (long long)righter[ter.first].second.second);
          } else {
            int shit = 1000000001;
            if (i == ter.first)
              shit = min(shit, queryr(1, 0, n - 1, ter.first + 1, ter.second));
            else if (i == ter.second)
              shit = min(shit, queryr(1, 0, n - 1, ter.first, ter.second - 1));
            else if (i > ter.first && i < ter.second)
              shit = min(shit, min(queryr(1, 0, n - 1, ter.first, i - 1),
                                   queryr(1, 0, n - 1, i + 1, ter.second)));
            else
              shit = min(shit, queryr(1, 0, n - 1, ter.first, ter.second));
            mini =
                min(mini, (long long)lefter[i].second.second + (long long)shit);
          }
        }
      }
    }
  }
  if (mini != 2LL * (long long)1000000001)
    cout << mini;
  else
    cout << -1;
  return 0;
}
