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;
  long long inf = 4e18, mod = 1e9 + 7;
  int segt[8000005], lazy[8000005];
  void push(int v, int l, int r) {
    segt[v] += lazy[v];
    if (l != r) {
      lazy[2 * v] += lazy[v];
      lazy[2 * v + 1] += lazy[v];
    }
    lazy[v] = 0;
  }
  void update(int v, int l, int r, int qs, int qe, int x) {
    push(v, l, r);
    if (r < qs || l > qe || qs > qe) return;
    if (qs <= l && r <= qe) {
      lazy[v] += x;
      push(v, l, r);
      return;
    }
    int m = (l + r) / 2;
    update(2 * v, l, m, qs, qe, x);
    update(2 * v + 1, m + 1, r, qs, qe, x);
    segt[v] = min(segt[2 * v], segt[2 * v + 1]);
  }
  int query(int v, int l, int r, int qs, int qe) {
    push(v, l, r);
    if (r < qs || l > qe || qs > qe) return mod;
    if (qs <= l && r <= qe) return segt[v];
    int m = (l + r) / 2;
    int v1 = query(2 * v, l, m, qs, qe);
    int v2 = query(2 * v + 1, m + 1, r, qs, qe);
    return min(v1, v2);
  }
  void solve() {
    int n, m;
    cin >> n >> m;
    m *= 2;
    vector<vector<int>> v;
    for (int i = 1; i <= n; i++) {
      int l, r, w;
      cin >> l >> r >> w;
      l *= 2, r *= 2;
      v.push_back({w, l, r});
    }
    sort(v.begin(), v.end());
    int r = 0, res = mod;
    for (int l = 1; l <= n; l++) {
      while (r < n && query(1, 1, m, 2, m) == 0) {
        r++;
        int lf = v[r - 1][1], rf = v[r - 1][2];
        update(1, 1, m, lf, rf, 1);
      }
      if (query(1, 1, m, 2, m) > 0) {
        res = min(res, v[r - 1][0] - v[l - 1][0]);
      }
      int lf = v[l - 1][1], rf = v[l - 1][2];
      update(1, 1, m, lf, rf, -1);
    }
    cout << res << endl;
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    while (t--) solve();
  }
  