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 int dim = 300009, dim1 = 4000009, inf = 1e9;
  struct seg {
    int l, r, w;
  } v[dim];
  int aint[dim1], lazy[dim1];
  void push(int nod) {
    if (2 * nod + 1 < dim1) {
      lazy[2 * nod] += lazy[nod];
      lazy[2 * nod + 1] += lazy[nod];
    }
    aint[nod] += lazy[nod];
    lazy[nod] = 0;
  }
  void Update(int nod, int st, int dr, int l, int r, int val) {
    push(nod);
    if (l >= r) return;
    if (st == l && dr == r) {
      lazy[nod] += val;
      push(nod);
      return;
    }
    int mij = (st + dr) / 2;
    Update(2 * nod, st, mij, l, min(mij, r), val);
    Update(2 * nod + 1, mij, dr, max(mij, l), r, val);
    aint[nod] = min(aint[2 * nod], aint[2 * nod + 1]);
  }
  bool cmp(seg a, seg b) { return a.w < b.w; }
  int get() { return aint[1] + lazy[1]; }
  signed main() {
    int n, m, ans = inf;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      cin >> v[i].l >> v[i].r >> v[i].w;
      v[i].l--;
      v[i].r--;
    }
    m--;
    sort(v, v + n, cmp);
    int j = 0;
    for (int i = 0; i < n; i++) {
      while (j < n && get() == 0) {
        Update(1, 0, m, v[j].l, v[j].r, 1);
        j++;
      }
      if (get() == 0) {
        break;
      }
      ans = min(ans, v[j - 1].w - v[i].w);
      Update(1, 0, m, v[i].l, v[i].r, -1);
    }
    cout << ans;
  }
  