description: You are given n segments on a number line, numbered from 1 to n. The i-th segments covers all integer points from l_i to r_i and has a value w_i.
  
  You are asked to select a subset of these segments (possibly, all of them). Once the subset is selected, it's possible to travel between two integer points if there exists a selected segment that covers both of them. A subset is good if it's possible to reach point m starting from point 1 in arbitrary number of moves.
  
  The cost of the subset is the difference between the maximum and the minimum values of segments in it. Find the minimum cost of a good subset.
  
  In every test there exists at least one good subset.
  
  Input
  
  The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5; 2 ≤ m ≤ 10^6) — the number of segments and the number of integer points.
  
  Each of the next n lines contains three integers l_i, r_i and w_i (1 ≤ l_i < r_i ≤ m; 1 ≤ w_i ≤ 10^6) — the description of the i-th segment.
  
  In every test there exists at least one good subset.
  
  Output
  
  Print a single integer — the minimum cost of a good subset.
  
  Examples
  
  Input
  
  
  5 12
  1 5 5
  3 4 10
  4 10 6
  11 12 5
  10 12 3
  
  
  Output
  
  
  3
  
  
  Input
  
  
  1 10
  1 10 23
  
  
  Output
  
  
  0
solution: #include <bits/stdc++.h>
  using namespace std;
  mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  template <typename A, typename B>
  ostream& operator<<(ostream& cout, pair<A, B> const& p) {
    return cout << "(" << p.first << ", " << p.second << ")";
  }
  template <typename A>
  ostream& operator<<(ostream& cout, vector<A> const& v) {
    cout << "[";
    for (int i = 0; i < (int)v.size(); i++) {
      if (i) cout << ", ";
      cout << v[i];
    }
    return cout << "]";
  }
  template <typename T>
  bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0;
  }
  template <typename T>
  bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0;
  }
  template <class T>
  struct LazySeg {
    std::vector<T> tree, lazy;
    int n;
    LazySeg() {}
    LazySeg(int _n) { init(_n); }
    LazySeg(vector<T>& arr) {
      init(arr.size());
      init(arr);
    }
    void init(int _n) {
      n = _n;
      tree.assign(4 * n, 0);
      lazy.assign(4 * n, 0);
    }
    void init(vector<T>& arr, int i = 0, int tl = 0, int tr = -1) {
      if (tr == -1) tr = n - 1;
      if (tl == tr) {
        tree[i] = arr[tl];
        return;
      }
      int mid = (tl + tr) / 2;
      init(arr, 2 * i + 1, tl, mid);
      init(arr, 2 * i + 2, mid + 1, tr);
      pull(i);
    }
    const T ID = 1000000007;
    void push(int i, int tl, int tr) {
      tree[i] += lazy[i];
      if (tl != tr) {
        lazy[2 * i + 1] += lazy[i];
        lazy[2 * i + 2] += lazy[i];
      }
      lazy[i] = 0;
    }
    T comb(T a, T b) { return min(a, b); }
    void pull(int i) { tree[i] = comb(tree[2 * i + 1], tree[2 * i + 2]); }
    void upd(int ql, int qr, T v, int i = 0, int tl = 0, int tr = -1) {
      if (tr == -1) tr = n - 1;
      push(i, tl, tr);
      if (qr < tl || tr < ql) return;
      if (ql <= tl && tr <= qr) {
        lazy[i] = v;
        push(i, tl, tr);
        return;
      }
      int mid = (tl + tr) / 2;
      upd(ql, qr, v, 2 * i + 1, tl, mid);
      upd(ql, qr, v, 2 * i + 2, mid + 1, tr);
      pull(i);
    }
    T query(int ql, int qr, int i = 0, int tl = 0, int tr = -1) {
      if (tr == -1) tr = n - 1;
      push(i, tl, tr);
      if (ql > tr || tl > qr) return ID;
      if (ql <= tl && tr <= qr) return tree[i];
      int mid = (tl + tr) / 2;
      return comb(query(ql, qr, 2 * i + 1, tl, mid),
                  query(ql, qr, 2 * i + 2, mid + 1, tr));
    }
  };
  int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    vector<pair<int, pair<int, int>>> v(n);
    for (int i = 0; i < (n); i++) {
      cin >> v[i].second.first >> v[i].second.second >> v[i].first;
    }
    sort(v.begin(), v.end());
    LazySeg<int> st(m);
    int r = 0;
    int best = 1000000007;
    for (int i = 0; i < (n); i++) {
      while (r < n && st.query(0, m - 2) == 0) {
        st.upd(v[r].second.first - 1, v[r].second.second - 2, 1);
        r++;
      }
      if (st.query(0, m - 2)) {
        ckmin(best, v[r - 1].first - v[i].first);
      }
      st.upd(v[i].second.first - 1, v[i].second.second - 2, -1);
    }
    cout << best << "\n";
  }
  