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 N = 3 * 1e5 + 100, M = 1e6 + 100;
  int n, m, MIN[M * 4], tag[M * 4];
  struct node {
    int l, r, w;
  } sh[N];
  bool operator<(node a, node b) { return a.w < b.w; }
  void pushup(int x) { MIN[x] = min(MIN[x + x], MIN[x + x + 1]); }
  void pushdown(int x) {
    if (tag[x]) {
      MIN[x + x] += tag[x];
      MIN[x + x + 1] += tag[x];
      tag[x + x] += tag[x];
      tag[x + x + 1] += tag[x];
      tag[x] = 0;
    }
  }
  void change(int x, int l, int r, int ll, int rr, int v) {
    if (ll <= l && rr >= r) {
      MIN[x] += v;
      tag[x] += v;
      return;
    }
    int mid = (l + r) >> 1;
    pushdown(x);
    if (ll <= mid) change(x + x, l, mid, ll, rr, v);
    if (rr > mid) change(x + x + 1, mid + 1, r, ll, rr, v);
    pushup(x);
  }
  inline int read() {
    int f = 1, x = 0;
    char s = getchar();
    while (s < '0' || s > '9') {
      if (s == '-') f = -1;
      s = getchar();
    }
    while (s >= '0' && s <= '9') {
      x = x * 10 + s - '0';
      s = getchar();
    }
    return x * f;
  }
  void add(int x, int v) {
    if (!x) return;
    change(1, 1, m - 1, sh[x].l, sh[x].r - 1, v);
  }
  signed main() {
    n = read();
    m = read();
    for (int i = 1; i <= n; i++) sh[i] = (node){read(), read(), read()};
    sort(sh + 1, sh + 1 + n);
    int l = n, r = n, ans = 0x3f3f3f3f;
    add(n, 1);
    if (MIN[1] != 0) ans = 0;
    while (l <= r && l >= 1) {
      if (MIN[1] == 0)
        add(--l, 1);
      else
        add(r--, -1);
      if (MIN[1] != 0) ans = min(ans, sh[r].w - sh[l].w);
    }
    printf("%d\n", ans);
  }
  