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;
  long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
  void input() {
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  }
  const int N = 1e6 + 10, M = N * 2, inf = 1e8;
  struct Node {
    int l, r, w;
  } a[N];
  int n, m;
  bool com(Node n1, Node n2) { return n1.w < n2.w; }
  struct Segment {
    int l, r, v, lazy;
  } 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].lazy) {
      tr[u << 1].lazy += tr[u].lazy;
      tr[u << 1 | 1].lazy += tr[u].lazy;
      tr[u << 1].v += tr[u].lazy;
      tr[u << 1 | 1].v += tr[u].lazy;
      tr[u].lazy = 0;
    }
  }
  void build(int u, int l, int r) {
    tr[u].l = l, tr[u].r = r, tr[u].lazy = 0, tr[u].v = 0;
    if (l == r)
      return;
    else {
      int mid = (l + r) >> 1;
      build(u << 1, l, mid);
      build(u << 1 | 1, mid + 1, r);
    }
  }
  void update(int u, int l, int r, int v) {
    if (l <= tr[u].l && tr[u].r <= r) {
      tr[u].v += v;
      tr[u].lazy += v;
    } else {
      pushdown(u);
      int mid = (tr[u].l + tr[u].r) >> 1;
      if (l <= mid) update(u << 1, l, r, v);
      if (r > mid) update(u << 1 | 1, l, r, v);
      pushup(u);
    }
  }
  int query(int u, int l, int r) {
    if (l <= tr[u].l && tr[u].r <= r) {
      return tr[u].v;
    } else {
      pushdown(u);
      int mid = (tr[u].l + tr[u].r) >> 1, ans = 1e9;
      if (l <= mid) ans = min(ans, query(u << 1, l, r));
      if (r > mid) ans = min(ans, query(u << 1 | 1, l, r));
      return ans;
    }
  }
  int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    int maxm = m + 10;
    for (int i = 1; i <= n; i++)
      cin >> a[i].l >> a[i].r >> a[i].w, maxm = max(maxm, a[i].r), a[i].r--;
    sort(a + 1, a + n + 1, com);
    build(1, 1, maxm);
    int ans = 1e9;
    for (int i = 1, l = 1; i <= n; i++) {
      update(1, a[i].l, a[i].r, 1);
      while (l <= i && query(1, 1, m - 1)) {
        ans = min(ans, a[i].w - a[l].w);
        update(1, a[l].l, a[l].r, -1);
        l++;
      }
    }
    cout << ans << '\n';
    return 0;
  }
  