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 mod = 10000019;
  const int maxn = 1e6 + 5;
  struct {
    int laz, sum, m;
  } tr[maxn * 4];
  struct node {
    int l, r, w;
    bool operator<(const node& x) const { return w < x.w; }
  } no[1000005];
  int n, m;
  void pushdown(int a, int b, int x) {
    if (a == b) {
      tr[x].m = tr[x].sum = x;
      return;
    }
    int laz = tr[x].laz, mid = (a + b) / 2;
    tr[x].laz = 0;
    tr[x << 1].sum += (mid - a + 1) * laz;
    tr[x << 1].laz += laz;
    tr[x << 1].m += laz;
    tr[(x << 1) | 1].sum += (b - mid) * laz;
    tr[(x << 1) | 1].laz += laz;
    tr[(x << 1) | 1].m += laz;
  }
  void pushup(int a, int b, int x) {
    if (a == b) {
      tr[x].m = tr[x].sum = x;
      return;
    }
    tr[x].m = min(tr[x << 1].m, tr[(x << 1) | 1].m);
    tr[x].sum = tr[x << 1].sum + tr[(x << 1) | 1].sum;
  }
  void upd(int a, int b, int l, int r, int x, int v) {
    if (a >= l && b <= r) {
      tr[x].laz += v;
      tr[x].sum += v * (b - a + 1);
      tr[x].m += v;
      return;
    }
    int m = (a + b) / 2;
    pushdown(a, b, x);
    if (m >= l) upd(a, m, l, r, x << 1, v);
    if (m < r) upd(m + 1, b, l, r, (x << 1) | 1, v);
    pushup(a, b, x);
  }
  int que(int a, int b, int l, int r, int x) {
    if (a >= l && b <= r) {
      return tr[x].m;
    }
    int m = (a + b) / 2;
    pushdown(a, b, x);
    int res = maxn;
    if (m >= l) res = min(res, que(a, m, l, r, x << 1));
    if (m < r) res = min(res, que(m + 1, b, l, r, (x << 1) | 1));
    return res;
  }
  bool check(int x) {
    int m = que(1, m, no[x].l, no[x].r, 1);
    if (m > 1) return true;
    return false;
  }
  void go(int x) { upd(1, m, no[x].l, no[x].r, 1, -1); }
  int main() {
    memset(tr, 0, sizeof tr);
    memset(no, 0, sizeof no);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      cin >> no[i].l >> no[i].r >> no[i].w;
    }
    sort(no + 1, no + n + 1);
    int l = 1, r = 1, res = 10000000;
    --m;
    for (l = 1; l <= n; l++) {
      while (r <= n) {
        if (que(1, m, 1, m, 1) < 1) {
          upd(1, m, no[r].l, no[r].r - 1, 1, 1);
          ++r;
        } else
          break;
      }
      if (que(1, m, 1, m, 1) < 1) break;
      res = min(res, no[r - 1].w - no[l].w);
      upd(1, m, no[l].l, no[l].r - 1, 1, -1);
    }
    cout << res << endl;
  }
  