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());
  const int P = 1e9 + 7;
  const int INF = 0x3f3f3f3f;
  const int N = 1e6 + 10;
  const double eps = 1e-8;
  int n, m;
  int minv[N << 2], tag[N << 2];
  vector<pair<int, pair<int, int>>> v;
  void push_down(int u) {
    if (tag[u]) {
      tag[(u << 1)] = tag[((u << 1) | 1)] = tag[u];
      minv[(u << 1)] = minv[((u << 1) | 1)] = tag[u];
      tag[u] = 0;
    }
  }
  void modify(int u, int tl, int tr, int L, int R, int d) {
    if (tl >= L && tr <= R)
      minv[u] = tag[u] = d;
    else {
      push_down(u);
      if (L <= ((tl + tr) >> 1)) modify((u << 1), tl, ((tl + tr) >> 1), L, R, d);
      if (R > ((tl + tr) >> 1))
        modify(((u << 1) | 1), ((tl + tr) >> 1) + 1, tr, L, R, d);
      minv[u] = min(minv[(u << 1)], minv[((u << 1) | 1)]);
    }
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    m--;
    for (int i = 1; i <= n; ++i) {
      int l, r, w;
      cin >> l >> r >> w;
      r--;
      v.push_back({w, {l, r}});
    }
    sort(v.begin(), v.end());
    int res = INF;
    for (auto seg : v) {
      modify(1, 1, m, seg.second.first, seg.second.second, seg.first);
      if (minv[1]) res = min(res, seg.first - minv[1]);
    }
    cout << res << endl;
    return 0;
  }
  