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;
  int n, m;
  struct node {
    int l, r, w;
  } a[300010];
  bool cmp(node x, node y) { return x.w < y.w; }
  int tree[4000010], tag[4000010];
  void pushdown(int now) {
    tag[now << 1] += tag[now];
    tag[now << 1 | 1] += tag[now];
    tree[now << 1] += tag[now];
    tree[now << 1 | 1] += tag[now];
    tag[now] = 0;
  }
  void modify(int now, int l, int r, int ql, int qr, int val) {
    if (ql <= l && qr >= r) {
      tree[now] += val, tag[now] += val;
      return;
    }
    pushdown(now);
    int mid = (l + r) >> 1;
    if (ql <= mid) modify(now << 1, l, mid, ql, qr, val);
    if (mid < qr) modify(now << 1 | 1, mid + 1, r, ql, qr, val);
    tree[now] = min(tree[now << 1], tree[now << 1 | 1]);
  }
  int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i].l >> a[i].r >> a[i].w;
    sort(a + 1, a + 1 + n, cmp);
    int lb, rb;
    lb = 1, rb = 1;
    modify(1, 1, m - 1, a[1].l, a[1].r, 1);
    int ans = 114514114;
    while (rb <= n) {
      if (tree[1] > 0) {
        ans = min(ans, a[rb].w - a[lb].w);
        if (ans == 0) {
          cout << 0;
          return 0;
        }
        modify(1, 1, m - 1, a[lb].l, a[lb].r - 1, -1);
        lb++;
      } else {
        rb++;
        if (rb <= n) modify(1, 1, m - 1, a[rb].l, a[rb].r - 1, 1);
      }
    }
    cout << ans << endl;
    return 0;
  }
  