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;
  template <class T>
  ostream &operator<<(ostream &out, vector<T> &v) {
    out << "[";
    for (auto k : v) out << k << " ";
    out << "]"
        << "\n";
    return out;
  }
  template <class T>
  ostream &operator<<(ostream &out, set<T> s) {
    out << "{";
    for (auto k : s) out << k << " ";
    out << "}"
        << "\n";
    return out;
  }
  template <class T, class U>
  ostream &operator<<(ostream &out, pair<T, U> p) {
    out << "[ " << p.first << " , " << p.second << " ] ";
    return out;
  }
  template <class T, class U>
  istream &operator>>(istream &in, pair<T, U> &p) {
    in >> p.first >> p.second;
    return in;
  }
  struct STree {
    vector<int> st, lazy;
    int n;
    STree(int n) : st(4 * n + 5, 0), lazy(4 * n + 5, 0), n(n) {}
    void init(int k, int s, int e, int *a) {
      lazy[k] = 0;
      if (s + 1 == e) {
        st[k] = a[s];
        return;
      }
      int m = (s + e) / 2;
      init(2 * k, s, m, a);
      init(2 * k + 1, m, e, a);
      st[k] = min(st[2 * k], st[2 * k + 1]);
    }
    void push(int k, int s, int e) {
      if (!lazy[k]) return;
      st[k] += lazy[k];
      if (s + 1 < e) {
        lazy[2 * k] += lazy[k];
        lazy[2 * k + 1] += lazy[k];
      }
      lazy[k] = 0;
    }
    void upd(int k, int s, int e, int a, int b, int v) {
      push(k, s, e);
      if (s >= b || e <= a) return;
      if (s >= a && e <= b) {
        lazy[k] += v;
        push(k, s, e);
        return;
      }
      int m = (s + e) / 2;
      upd(2 * k, s, m, a, b, v);
      upd(2 * k + 1, m, e, a, b, v);
      st[k] = min(st[2 * k], st[2 * k + 1]);
    }
    int query(int k, int s, int e, int a, int b) {
      if (s >= b || e <= a) return 1e9;
      push(k, s, e);
      if (s >= a && e <= b) return st[k];
      int m = (s + e) / 2;
      return min(query(2 * k, s, m, a, b), query(2 * k + 1, m, e, a, b));
    }
    void init(int *a) { init(1, 0, n, a); }
    void upd(int a, int b, int v) { upd(1, 0, n, a, b, v); }
    int query(int a, int b) { return query(1, 0, n, a, b); }
  };
  struct seg {
    int w, l, r;
    void Leer() { cin >> l >> r >> w; }
  };
  bool operator<(const seg &a, const seg &b) { return a.w < b.w; }
  seg in[500000];
  int main() {
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    m++;
    STree st(m);
    for (int i = 0; i < int(n); i++) in[i].Leer();
    sort(in, in + n);
    int i = 0, j = -1;
    int res = 2e6;
    while (i < n) {
      if (st.query(1, m - 1) > 0) {
        res = min(res, in[j].w - in[i].w);
        st.upd(in[i].l, in[i].r, -1);
        i++;
      } else {
        if (j == n - 1) break;
        j++;
        st.upd(in[j].l, in[j].r, 1);
      }
    }
    cout << res << '\n';
    return 0;
  }
  