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(chrono::steady_clock::now().time_since_epoch().count());
  struct lazyseg {
    int base = INT_MAX;
    int comb(int a, int b) { return min(a, b); }
    struct node {
      int val, lazy;
    };
    vector<node> st;
    lazyseg(int n) : st(4 * n + 10) {}
    void puttag(int k, int l, int r, int x) {
      st[k].val += x;
      st[k].lazy += x;
    }
    void pushdown(int k, int l, int r) {
      if (l != r) {
        int m = (l + r) / 2;
        puttag(2 * k, l, m, st[k].lazy);
        puttag(2 * k + 1, m + 1, r, st[k].lazy);
      }
      st[k].lazy = 0;
    }
    void update(int a, int b, int k, int l, int r, int x) {
      if (a <= l && r <= b) {
        puttag(k, l, r, x);
        return;
      }
      pushdown(k, l, r);
      int m = (l + r) / 2;
      if (a <= m) update(a, b, 2 * k, l, m, x);
      if (b > m) update(a, b, 2 * k + 1, m + 1, r, x);
      st[k].val = comb(st[2 * k].val, st[2 * k + 1].val);
    }
    int get(int a, int b, int k, int l, int r) {
      if (a <= l && r <= b) return st[k].val;
      pushdown(k, l, r);
      int m = (l + r) / 2;
      int ret = base;
      if (a <= m) ret = comb(ret, get(a, b, 2 * k, l, m));
      if (b > m) ret = comb(ret, get(a, b, 2 * k + 1, m + 1, r));
      return ret;
    }
    void build(vector<int> &v, int k, int l, int r) {
      if (l == r) {
        st[k].val = v[l];
        return;
      }
      int m = (l + r) / 2;
      build(v, 2 * k, l, m);
      build(v, 2 * k + 1, m + 1, r);
      st[k].val = comb(st[2 * k].val, st[2 * k + 1].val);
    }
  };
  const int mx = 2e6 + 5;
  int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    m *= 2;
    vector<array<int, 3>> v;
    for (int q = 0; q < n; q++) {
      int a, b, c;
      cin >> a >> b >> c;
      a *= 2;
      b *= 2;
      v.push_back({c, a, b});
    }
    sort(v.begin(), v.end());
    lazyseg st(mx);
    int lo = -1;
    int ans = INT_MAX;
    for (int q = 0; q < n; q++) {
      st.update(v[q][1], v[q][2], 1, 0, mx - 1, 1);
      if (lo < 0) lo++;
      while (lo < n && st.get(2, m, 1, 0, mx - 1) > 0) {
        if (lo >= 0) {
          st.update(v[lo][1], v[lo][2], 1, 0, mx - 1, -1);
        }
        if (st.get(2, m, 1, 0, mx - 1) > 0) {
          lo++;
          continue;
        }
        if (lo >= 0) {
          st.update(v[lo][1], v[lo][2], 1, 0, mx - 1, 1);
        }
        break;
      }
      if (st.get(2, m, 1, 0, mx - 1) > 0) ans = min(ans, v[q][0] - v[lo][0]);
    }
    cout << ans << '\n';
    return 0;
  }
  