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 rnd(time(0));
  long long mod = 1000000007;
  long long inf = 1000000000000000;
  const long long MAXN = 2e5 + 10;
  struct SegmentTree {
    long long R;
    vector<long long> mns, t;
    void update(long long x, long long xl, long long xr, long long ql,
                long long qr, long long val) {
      if (xl >= qr || ql >= xr) return;
      if (ql <= xl && xr <= qr) {
        t[x] += val;
        mns[x] += val;
        return;
      }
      long long mid = (xl + xr) >> 1;
      update(x * 2, xl, mid, ql, qr, val);
      update(x * 2 + 1, mid, xr, ql, qr, val);
      mns[x] = t[x] + min(mns[x * 2], mns[x * 2 + 1]);
    }
    void upd(tuple<long long, long long, long long> segment, long long val) {
      update(1, 0, R, get<1>(segment) - 1, get<2>(segment) - 1, val);
    }
    SegmentTree(long long m) {
      R = 1;
      while (R < m) R *= 2;
      mns.assign(2 * R, 0);
      t.assign(2 * R, 0);
      update(1, 0, R, m - 1, R, 1);
    }
  };
  void solve() {
    long long n, m;
    cin >> n >> m;
    vector<tuple<long long, long long, long long>> a(n);
    for (long long i = 0; i < n; ++i) {
      long long l, r, w;
      cin >> l >> r >> w;
      a[i] = {w, l, r};
    }
    sort(a.begin(), a.end());
    SegmentTree sg(m);
    long long r = 0, ans = inf;
    for (long long l = 0; l < n; ++l) {
      while (r < n && sg.mns[1] == 0) sg.upd(a[r++], 1);
      if (sg.mns[1] != 0) ans = min(ans, get<0>(a[r - 1]) - get<0>(a[l]));
      sg.upd(a[l], -1);
    }
    cout << ans << "\n";
  }
  signed main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    solve();
    return 0;
  }
  