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: import java.io.*;
  import java.util.*;
  
  public class CF1555E extends PrintWriter {
  	CF1555E() { super(System.out, true); }
  	Scanner sc = new Scanner(System.in);
  	public static void main(String[] $) {
  		CF1555E o = new CF1555E(); o.main(); o.flush();
  	}
  
  	int[] st; boolean[] covered; int n_;
  	void pul(int i) {
  		covered[i] = st[i] > 0 || i < n_ && covered[i << 1] && covered[i << 1 | 1];
  	}
  	void init(int n) {
  		n_ = 1;
  		while (n_ < n)
  			n_ <<= 1;
  		st = new int[n_ * 2];
  		covered = new boolean[n_ * 2];
  		for (int i = n_ + n; i < n_ + n_; i++) {
  			st[i] = 1;
  			covered[i] = true;
  		}
  		for (int i = n_; i >= 1; i--)
  			pul(i);
  	}
  	void update(int i, int x) {
  		st[i] += x;
  		pul(i);
  		while (i > 1 && covered[i >> 1] != covered[i])
  			pul(i >>= 1);
  	}
  	void update(int l, int r, int x) {
  		for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
  			if ((l & 1) == 1)
  				update(l++, x);
  			if ((r & 1) == 0)
  				update(r--, x);
  		}
  	}
  	void main() {
  		int n = sc.nextInt();
  		int m = sc.nextInt() - 1;
  		int[] ll = new int[n];
  		int[] rr = new int[n];
  		int[] ww = new int[n];
  		Integer[] ii = new Integer[n];
  		for (int i = 0; i < n; i++) {
  			ll[i] = sc.nextInt() - 1;
  			rr[i] = sc.nextInt() - 1 - 1;
  			ww[i] = sc.nextInt();
  			ii[i] = i;
  		}
  		init(m);
  		Arrays.sort(ii, (i, j) -> ww[i] - ww[j]);
  		int ans = ww[ii[n - 1]] - ww[ii[0]];
  		for (int i = 0, j = 0; i < n; i++) {
  			while (j < n && !covered[1]) {
  				update(ll[ii[j]], rr[ii[j]], 1);
  				j++;
  			}
  			if (!covered[1])
  				break;
  			ans = Math.min(ans, ww[ii[j - 1]] - ww[ii[i]]);
  			update(ll[ii[i]], rr[ii[i]], -1);
  		}
  		println(ans);
  	}
  }
  