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 = 1000007;
  const int mod = 1e9 + 7;
  const int inf = 0x3f3f3f3f3f3f3f3f;
  const double eps = 1e-8;
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, 1, 0, -1};
  int n, m, k, t, q, p;
  int vis[N], b[N];
  struct Node {
    int w, l, r;
  } a[N];
  bool cmp(Node a, Node b) { return a.w < b.w; }
  struct tree {
    int l, r, v, add;
  } tr[N * 4];
  void pushup(int u) { tr[u].v = min(tr[u << 1].v, tr[u << 1 | 1].v); }
  void pushdown(int u) {
    if (tr[u].add != 0) {
      tr[u << 1].add += tr[u].add;
      tr[u << 1 | 1].add += tr[u].add;
      tr[u << 1].v += tr[u].add;
      tr[u << 1 | 1].v += tr[u].add;
      tr[u].add = 0;
    }
  }
  void build(int u, int l, int r) {
    tr[u].l = l, tr[u].r = r;
    tr[u].add = 0;
    if (l == r) {
      tr[u].v = 0;
      return;
    }
    int mid = l + r >> 1;
    if (l <= mid) build(u << 1, l, mid);
    if (r > mid) build(u << 1 | 1, mid + 1, r);
    pushup(u);
  }
  void modify2(int u, int l, int r, int w) {
    if (l > r) return;
    if (l <= tr[u].l && r >= tr[u].r) {
      tr[u].add += w;
      tr[u].v += w;
      return;
    }
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid) modify2(u << 1, l, r, w);
    if (r > mid) modify2(u << 1 | 1, l, r, w);
    pushup(u);
  }
  int query(int u, int l, int r) {
    if (l <= tr[u].l && r >= tr[u].r) {
      return tr[u].v;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    int res = inf;
    if (l <= mid) res = min(res, query(u << 1, l, r));
    if (r > mid) res = min(res, query(u << 1 | 1, l, r));
    return res;
  }
  signed main() {
    cin >> n >> m;
    int cnt = 0;
    int maxm = 0;
    for (int i = 1; i <= n; i++) {
      int l, r, w;
      cin >> l >> r >> w;
      maxm = max(maxm, r);
      r--;
      a[++cnt] = {w, l, r};
    }
    n = cnt;
    sort(a + 1, a + 1 + n, cmp);
    build(1, 1, maxm);
    int res = inf;
    for (int i = 1, j = 0; i <= n; i++) {
      while (j < n && query(1, 1, m - 1) <= 0) {
        j++;
        modify2(1, a[j].l, a[j].r, 1);
      }
      if (query(1, 1, m - 1) >= 1) res = min(res, a[j].w - a[i].w);
      if (i == n) break;
      modify2(1, a[i].l, a[i].r, -1);
    }
    cout << res << "\n";
    return 0;
  }
  