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 qr() {
    char ch = getchar();
    int x = 1, s = 0;
    while (ch < '0' || ch > '9') {
      if (ch == '-') x = -1;
      ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
      s = s * 10 + ch - '0';
      ch = getchar();
    }
    return x * s;
  }
  int n, m, tree[1000002 << 2], tag[1000002 << 2], ans = 1e6, r;
  struct node {
    int l, r, w;
  } seg[300002];
  bool cmp(node a, node b) { return a.w < b.w; }
  int minn(int a, int b) { return a < b ? a : b; }
  void upd(int id) { tree[id] = minn(tree[id << 1], tree[id << 1 | 1]); }
  void pushdown(int id) {
    int ls = id << 1, rs = id << 1 | 1;
    tree[ls] += tag[id];
    tag[ls] += tag[id];
    tree[rs] += tag[id];
    tag[rs] += tag[id];
    tag[id] = 0;
  }
  void add(int l, int r, int i, int j, int id, int d) {
    if (l <= i && j <= r) {
      tree[id] += d;
      tag[id] += d;
      return;
    }
    int mid = (i + j) >> 1, ls = id << 1, rs = id << 1 | 1;
    pushdown(id);
    if (l <= mid) add(l, r, i, mid, ls, d);
    if (r > mid) add(l, r, mid + 1, j, rs, d);
    upd(id);
  }
  int main() {
    n = qr();
    m = qr();
    for (int i = 1; i <= n; i++) {
      seg[i].l = qr();
      seg[i].r = qr();
      seg[i].w = qr();
    }
    sort(seg + 1, seg + n + 1, cmp);
    for (int i = 1; i <= n; i++) {
      while (r < n && tree[1] == 0) {
        r++;
        add(seg[r].l, seg[r].r - 1, 1, m - 1, 1, 1);
      }
      if (tree[1] == 0) break;
      if (ans > seg[r].w - seg[i].w) ans = seg[r].w - seg[i].w;
      add(seg[i].l, seg[i].r - 1, 1, m - 1, 1, -1);
    }
    printf("%d", ans);
    return 0;
  }
  