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 int dx[] = {0, 1, 0, -1};
  long long int dy[] = {1, 0, -1, 0};
  long long int dxx[] = {0, 1, 0, -1, 1, 1, -1, -1};
  long long int dyy[] = {1, 0, -1, 0, 1, -1, 1, -1};
  const int mxx = 1000006;
  int tree[4 * mxx];
  void update(int l, int r, int L, int R, int node, int val) {
    if (l > R || r < L)
      return;
    else if (l >= L and r <= R) {
      tree[node] += val;
      return;
    }
    tree[node] -= min(tree[node << 1], tree[(node * 2 + 1)]);
    update(l, (l + r) / 2, L, R, node << 1, val);
    update((l + r) / 2 + 1, r, L, R, (node << 1) + 1, val);
    tree[node] += min(tree[node << 1], tree[(node << 1) + 1]);
  }
  int query(int l, int r, int L, int R, int node, int mx) {
    if (l > R || r < L)
      return 400000;
    else if (l >= L and r <= R) {
      return tree[node] + mx;
    }
    mx += (tree[node] - min(tree[node << 1], tree[node * 2 + 1]));
    int x = min(query(l, (l + r) / 2, L, R, node * 2, mx),
                query((l + r) / 2 + 1, r, L, R, node * 2 + 1, mx));
    return x;
  }
  int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    vector<pair<int, pair<int, int> > > v;
    for (int i = 1; i <= n; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      v.push_back({c, {a, b}});
    }
    sort(v.begin(), v.end());
    int ok = 0;
    int ans = 1000000;
    int j = 0;
    for (int i = 0; i < n; i++) {
      auto it = v[i];
      update(1, m - 1, it.second.first, it.second.second - 1, 1, 1);
      int x = tree[1];
      if (it.second.second == m) {
        ok = 1;
      }
      if (ok == 0 || x < 1) continue;
      while (query(1, m - 1, v[j].second.first, v[j].second.second - 1, 1, 0) >
             1) {
        update(1, m - 1, v[j].second.first, v[j].second.second - 1, 1, -1);
        j++;
      }
      ans = min(ans, v[i].first - v[j].first);
    }
    printf("%d\n", ans);
  }
  