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;
  int read() {
    int a = 0;
    int f = 0;
    char p = getchar();
    while (!isdigit(p)) {
      f |= p == '-';
      p = getchar();
    }
    while (isdigit(p)) {
      a = (a << 3) + (a << 1) + (p ^ 48);
      p = getchar();
    }
    return f ? -a : a;
  }
  const int INF = 998244353;
  int n, m, ans;
  struct node {
    int l, r, w;
    bool operator<(const node& p) const { return w < p.w; }
  } seg[1000050];
  int mn[4000050];
  int tag[4000050];
  void pushdown(int u) {
    mn[u << 1] += tag[u];
    tag[u << 1] += tag[u];
    mn[u << 1 | 1] += tag[u];
    tag[u << 1 | 1] += tag[u];
    tag[u] = 0;
  }
  void update(int u) { mn[u] = min(mn[u << 1], mn[u << 1 | 1]); }
  void modify(int u, int l, int r, int L, int R, int V) {
    if (L <= l && r <= R) {
      mn[u] += V;
      tag[u] += V;
      return;
    }
    int mid = (l + r) >> 1;
    pushdown(u);
    if (L <= mid) modify(u << 1, l, mid, L, R, V);
    if (R > mid) modify(u << 1 | 1, mid + 1, r, L, R, V);
    update(u);
  }
  int main() {
    ans = INF;
    n = read();
    m = read();
    for (int i = 1; i <= n; ++i) {
      seg[i].l = read();
      seg[i].r = read();
      seg[i].w = read();
    }
    sort(seg + 1, seg + n + 1);
    for (int i = 1, now = 0; i <= n; ++i) {
      while (mn[1] == 0 && now < n) {
        now++;
        modify(1, 2, m, seg[now].l + 1, seg[now].r, 1);
      }
      if (mn[1])
        ans = min(ans, seg[now].w - seg[i].w);
      else
        break;
      modify(1, 2, m, seg[i].l + 1, seg[i].r, -1);
    }
    printf("%d", ans);
    return 0;
  }
  