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>
  template <typename T>
  inline void rd(T& x) {
    int si = 1;
    char c = getchar();
    x = 0;
    while (!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
    while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
    x *= si;
  }
  template <typename T, typename... Args>
  inline void rd(T& x, Args&... args) {
    rd(x);
    rd(args...);
  }
  const int N = 1e6 + 5, Inf = 0x3f3f3f3f, Mod = 998244353;
  const long long InfLL = 0x3f3f3f3f3f3f3f3fLL;
  int seg[N << 3], tag[N << 3];
  void Update(int o) { seg[o] = std::min(seg[o << 1], seg[o << 1 | 1]); }
  void PushTag(int o) {
    if (tag[o]) {
      seg[o << 1] += tag[o];
      seg[o << 1 | 1] += tag[o];
      tag[o << 1] += tag[o];
      tag[o << 1 | 1] += tag[o];
      tag[o] = 0;
    }
  }
  void Modify(int o, int l, int r, int ql, int qr, int v) {
    if (ql > qr) return;
    if (ql <= l && r <= qr)
      seg[o] += v, tag[o] += v;
    else {
      int mid = (l + r) >> 1;
      PushTag(o);
      if (ql <= mid) Modify(o << 1, l, mid, ql, qr, v);
      if (qr > mid) Modify(o << 1 | 1, mid + 1, r, ql, qr, v);
      Update(o);
    }
  }
  struct Seg {
    int l, r;
    long long w;
  } I[N];
  int main() {
    int test_case_cnt = 1;
    while (test_case_cnt--) {
      int n, m;
      rd(n, m);
      for (int i = 1; i <= n; ++i) {
        rd(I[i].l, I[i].r, I[i].w);
      }
      std::sort(I + 1, I + n + 1,
                [](const Seg& x, const Seg& y) { return x.w < y.w; });
      long long ans = InfLL;
      for (int l = 1, r = 0; l <= n; ++l) {
        while (r + 1 <= n && !seg[1]) {
          ++r;
          Modify(1, 1, m - 1, I[r].l, I[r].r - 1, 1);
        }
        if (seg[1]) {
          ans = std::min(ans, I[r].w - I[l].w);
        }
        Modify(1, 1, m - 1, I[l].l, I[l].r - 1, -1);
      }
      printf("%lld\n", ans);
    }
    return 0;
  }
  