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;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  const int P = 1e9 + 7;
  const int INF = 0x3f3f3f3f;
  const int N = 1e6 + 10;
  int n, m;
  struct Line {
    int l, r, w;
    bool operator<(const Line &t) const { return w < t.w; }
  } line[N];
  struct Node {
    int l, r;
    int val, lazy;
  } tr[N * 4];
  void pushup(int u) { tr[u].val = min(tr[u << 1].val, tr[u << 1 | 1].val); }
  void pushdown(int u) {
    if (tr[u].lazy != -1) {
      tr[u << 1].val = tr[u << 1 | 1].val = tr[u].lazy;
      tr[u << 1].lazy = tr[u << 1 | 1].lazy = tr[u].lazy;
      tr[u].lazy = -1;
    }
  }
  void build(int u, int l, int r) {
    tr[u] = {l, r, 0, -1};
    if (l == r)
      return;
    else {
      int mid = l + r >> 1;
      build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
    }
  }
  void modify(int u, int l, int r, int d) {
    if (tr[u].l >= l && tr[u].r <= r)
      tr[u].val = tr[u].lazy = d;
    else {
      pushdown(u);
      int mid = tr[u].l + tr[u].r >> 1;
      if (l <= mid) modify(u << 1, l, r, d);
      if (r > mid) modify(u << 1 | 1, l, r, d);
      pushup(u);
    }
  }
  int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> line[i].l >> line[i].r >> line[i].w;
    build(1, 1, m - 1);
    sort(line + 1, line + n + 1);
    int res = INF;
    for (int i = 1; i <= n; ++i) {
      modify(1, line[i].l, line[i].r - 1, line[i].w);
      int t = tr[1].val;
      if (t) res = min(res, line[i].w - t);
    }
    cout << res << endl;
    return 0;
  }
  