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 long long maxm = 2e6 + 5;
  struct Node {
    long long l, r, w;
  } e[maxm];
  bool cmp(Node a, Node b) { return a.w < b.w; }
  struct Tree {
    long long mi[maxm << 2];
    long long laz[maxm << 2];
    void pp(long long node) { mi[node] = min(mi[node * 2], mi[node * 2 + 1]); }
    void pd(long long node) {
      if (laz[node]) {
        mi[node * 2] += laz[node];
        mi[node * 2 + 1] += laz[node];
        laz[node * 2] += laz[node];
        laz[node * 2 + 1] += laz[node];
        laz[node] = 0;
      }
    }
    void update(long long st, long long ed, long long val, long long l,
                long long r, long long node) {
      if (st <= l && ed >= r) {
        laz[node] += val;
        mi[node] += val;
        return;
      }
      pd(node);
      long long mid = (l + r) / 2;
      if (st <= mid) update(st, ed, val, l, mid, node * 2);
      if (ed > mid) update(st, ed, val, mid + 1, r, node * 2 + 1);
      pp(node);
    }
  } T;
  long long n, m;
  void solve() {
    cin >> n >> m;
    m--;
    for (long long i = 1; i <= n; i++) {
      long long l, r, w;
      cin >> l >> r >> w;
      r--;
      e[i] = {l, r, w};
    }
    sort(e + 1, e + 1 + n, cmp);
    long long ans = 1e18;
    long long l = 1;
    for (long long i = 1; i <= n; i++) {
      T.update(e[i].l, e[i].r, 1, 1, m, 1);
      while (T.mi[1] > 0) {
        ans = min(ans, e[i].w - e[l].w);
        T.update(e[l].l, e[l].r, -1, 1, m, 1);
        l++;
      }
    }
    cout << ans << endl;
  }
  signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
  }
  