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 INF = 0x3f3f3f3f;
  struct node {
    int l, r, val, lazy;
    node *left, *right;
  };
  node *build(int l, int r) {
    if (l == r) return new node{l, r, 0, 0, nullptr, nullptr};
    node *left = build(l, (l + r) / 2), *right = build((l + r) / 2 + 1, r);
    return new node{l, r, 0, 0, left, right};
  }
  void propagate(node *cur) {
    if (cur->lazy == 0) return;
    cur->val += cur->lazy;
    if (cur->left != nullptr) {
      cur->left->lazy += cur->lazy;
      cur->right->lazy += cur->lazy;
    }
    cur->lazy = 0;
  }
  int query(node *cur, int l, int r) {
    propagate(cur);
    if (l > cur->r || r < cur->l) return INF;
    if (l <= cur->l && cur->r <= r) return cur->val;
    return min(query(cur->left, l, r), query(cur->right, l, r));
  }
  void update(node *cur, int l, int r, int val) {
    propagate(cur);
    if (l > cur->r || r < cur->l) return;
    if (l <= cur->l && cur->r <= r) {
      cur->lazy += val;
      propagate(cur);
      return;
    }
    update(cur->left, l, r, val);
    update(cur->right, l, r, val);
    cur->val = min(cur->left->val, cur->right->val);
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int> > > a((int)1e6 + 1);
    for (int i = 0; i < n; i++) {
      int l, r, w;
      cin >> l >> r >> w;
      a[w].push_back({l, r - 1});
    }
    node *root = build(1, m);
    for (auto &p : a[1]) update(root, p.first, p.second, +1);
    int ans = INF;
    for (int l = 1, r = 1; l <= (int)1e6; l++) {
      if (r < l) r = l - 1;
      while (r < (int)1e6 && query(root, 1, m - 1) < 1) {
        r++;
        for (auto &p : a[r]) update(root, p.first, p.second, +1);
      }
      if (query(root, 1, m - 1) >= 1) ans = min(ans, r - l);
      for (auto &p : a[l]) update(root, p.first, p.second, -1);
    }
    cout << ans << '\n';
    return 0;
  }
  