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;
  const long long N = 1e6 + 5;
  long long n, m;
  long long a[N];
  long long mi[N << 2];
  long long tag[N << 2];
  inline long long lc(long long p) { return p << 1; }
  inline long long rc(long long p) { return p << 1 | 1; }
  void build(long long p, long long l, long long r) {
    if (l == r) {
      mi[p] = a[l];
      return;
    }
    long long mid = (l + r) >> 1;
    build(lc(p), l, mid);
    build(rc(p), mid + 1, r);
    mi[p] = min(mi[lc(p)], mi[rc(p)]);
  }
  void add(long long p, long long l, long long r, long long ql, long long qr,
           long long d) {
    if (ql <= l && r <= qr) {
      mi[p] += d;
      tag[p] += d;
      return;
    }
    long long mid = (l + r) >> 1;
    if (tag[p]) {
      tag[lc(p)] += tag[p];
      tag[rc(p)] += tag[p];
      mi[lc(p)] += tag[p];
      mi[rc(p)] += tag[p];
      tag[p] = 0;
    }
    if (ql <= mid) add(lc(p), l, mid, ql, qr, d);
    if (qr > mid) add(rc(p), mid + 1, r, ql, qr, d);
    mi[p] = min(mi[lc(p)], mi[rc(p)]);
  }
  long long find_mi(long long p, long long l, long long r, long long ql,
                    long long qr) {
    if (ql <= l && r <= qr) return mi[p];
    long long mid = (l + r) >> 1;
    if (tag[p]) {
      tag[lc(p)] += tag[p];
      tag[rc(p)] += tag[p];
      mi[lc(p)] += tag[p];
      mi[rc(p)] += tag[p];
      tag[p] = 0;
    }
    long long res = 1e9;
    if (ql <= mid) res = min(res, find_mi(lc(p), l, mid, ql, qr));
    if (qr > mid) res = min(res, find_mi(rc(p), mid + 1, r, ql, qr));
    return res;
  }
  struct Seg {
    long long l, r, w;
  } seg[N];
  bool cmp(Seg s1, Seg s2) { return s1.w < s2.w; }
  int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      long long L, R, W;
      cin >> L >> R >> W;
      seg[i] = (Seg){L, R, W};
    }
    sort(seg + 1, seg + n + 1, cmp);
    build(1, 1, m);
    long long i = 1, j = 0, ans = 1e9;
    long long ok = 0;
    while (1) {
      if (j > n || i > n) break;
      if (!ok) {
        j++;
        if (j > n) break;
        add(1, 1, m, seg[j].l, seg[j].r - 1, 1);
        if (find_mi(1, 1, m, 1, m - 1) > 0) {
          ans = min(ans, seg[j].w - seg[i].w);
          ok = 1;
        } else {
        }
      } else {
        add(1, 1, m, seg[i].l, seg[i].r - 1, -1);
        i++;
        if (i > n) break;
        if (find_mi(1, 1, m, 1, m - 1) > 0) {
          ans = min(ans, seg[j].w - seg[i].w);
        } else {
          ok = 0;
        }
      }
    }
    cout << ans << endl;
    return 0;
  }
  