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;
  struct node {
    int l, r, w;
  } len[2000017];
  int n, m, lazy[2000017 << 2], minn[2000017 << 2];
  bool cmp(node A, node B) {
    if (A.w == B.w) {
      if (A.l == B.l)
        return A.r < B.r;
      else
        return A.l < B.l;
    } else
      return A.w < B.w;
  }
  void modify(int k, int l, int r, int ql, int qr, int v) {
    if (l >= ql && r <= qr) {
      lazy[k] += v;
      minn[k] += v;
      return;
    }
    int mid = (l + r) >> 1;
    if (lazy[k]) {
      lazy[k << 1] += lazy[k];
      lazy[k << 1 | 1] += lazy[k];
      minn[k << 1] += lazy[k];
      minn[k << 1 | 1] += lazy[k];
      lazy[k] = 0;
    }
    if (ql <= mid) modify(k << 1, l, mid, ql, qr, v);
    if (mid < qr) modify(k << 1 | 1, mid + 1, r, ql, qr, v);
    minn[k] = min(minn[k << 1], minn[k << 1 | 1]);
  }
  int solve() {
    int nxt, lim = 2000017;
    for (int l = 1, r = 1; l <= n; l += 1) {
      while (!minn[1] && r <= n) {
        if (len[r].r != m)
          nxt = len[r].r - 1;
        else
          nxt = m;
        modify(1, 1, m, len[r].l, nxt, 1);
        r++;
      }
      if (minn[1]) lim = min(lim, len[r - 1].w - len[l].w);
      if (len[l].r != m)
        nxt = len[l].r - 1;
      else
        nxt = m;
      modify(1, 1, m, len[l].l, nxt, -1);
    }
    return lim;
  }
  int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i += 1)
      scanf("%d%d%d", &len[i].l, &len[i].r, &len[i].w);
    sort(len + 1, len + n + 1, cmp);
    printf("%d\n", solve());
    return 0;
  }
  