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 LL = long long;
  class SegTree {
    int n;
    std::vector<int> mx, tag;
    void pushDown(int p) {
      mx[p << 1] += tag[p];
      mx[p << 1 | 1] += tag[p];
      tag[p << 1] += tag[p];
      tag[p << 1 | 1] += tag[p];
      tag[p] = 0;
    }
    void pushUp(int p) { mx[p] = std::min(mx[p << 1], mx[p << 1 | 1]); }
    void modify(int x, int y, int val, int l, int r, int p) {
      if (x <= l && y >= r) {
        tag[p] += val;
        mx[p] += val;
        return;
      }
      pushDown(p);
      int m = (l + r) / 2;
      if (x < m) modify(x, y, val, l, m, p << 1);
      if (m < y) modify(x, y, val, m, r, p << 1 | 1);
      pushUp(p);
    }
  
   public:
    SegTree(int _n) : n(_n), mx(n * 4), tag(n * 4) {}
    void modify(int x, int y, int val) { modify(--x, y, val, 0, n, 1); }
    int max() { return mx[1]; }
  };
  void solve() {
    int n, m;
    std::cin >> n >> m;
    SegTree A(m - 1);
    std::vector<std::tuple<int, int, int>> a(n);
    for (auto &[w, l, r] : a) {
      std::cin >> l >> r >> w;
      --r;
    }
    std::sort(a.begin(), a.end());
    int ans = INT_MAX;
    for (int l = 0, r = 0; r < n; ++r) {
      A.modify(std::get<1>(a[r]), std::get<2>(a[r]), 1);
      while (A.max()) {
        ans = std::min(ans, std::get<0>(a[r]) - std::get<0>(a[l]));
        A.modify(std::get<1>(a[l]), std::get<2>(a[l]), -1);
        ++l;
      }
    }
    std::cout << ans << '\n';
  }
  int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int cas = 1;
    while (cas--) {
      solve();
    }
    return 0;
  }
  