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;
  void err(istream_iterator<string> it) { cerr << endl; }
  template <typename T, typename... Args>
  void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
  }
  const int inf = 0x3f3f3f3f;
  const int mod = 1e9 + 7;
  const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},
                         {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
  vector<int> t, ps;
  void push(int v) {
    t[v * 2] += ps[v];
    ps[v * 2] += ps[v];
    t[v * 2 + 1] += ps[v];
    ps[v * 2 + 1] += ps[v];
    ps[v] = 0;
  }
  void upd(int v, int tl, int tr, int l, int r, int val) {
    if (l > r) return;
    if (l == tl && r == tr) {
      t[v] += val;
      ps[v] += val;
    } else {
      push(v);
      int tm = (tl + tr) / 2;
      upd(v * 2, tl, tm, l, min(r, tm), val);
      upd(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r, val);
      t[v] = min(t[v * 2], t[v * 2 + 1]);
    }
  }
  int get() { return t[1]; }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<tuple<int, int, int>> a;
    for (int i = int(0); i < int(n); i++) {
      int l, r, w;
      cin >> l >> r >> w;
      --l, r -= 2;
      a.emplace_back(w, l, r);
    }
    --m;
    sort((a).begin(), (a).end(),
         [](tuple<int, int, int>& x, tuple<int, int, int>& y) {
           return get<0>(x) < get<0>(y);
         });
    t.resize(4 * m), ps.resize(4 * m);
    --m;
    int ans = inf;
    int j = 0;
    for (int i = int(0); i < int(n); i++) {
      while (j < n && get() == 0) {
        upd(1, 0, m, get<1>(a[j]), get<2>(a[j]), 1);
        ++j;
      }
      if (get() == 0) break;
      ans = min(ans, get<0>(a[j - 1]) - get<0>(a[i]));
      upd(1, 0, m, get<1>(a[i]), get<2>(a[i]), -1);
    }
    cout << ans << "\n";
    return 0;
  }
  