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.util.*;
  import java.io.*;
  
  public class E_1555 {
  	public static void main(String[] args) throws Exception {
  		Scanner sc = new Scanner(System.in);
  		PrintWriter pw = new PrintWriter(System.out);
  		
  		int n = sc.nextInt(), m = sc.nextInt();
  		Tri[] array = new Tri[n];
  		for(int i = 0; i < n; i++)
  			array[i] = new Tri(sc.nextInt(), sc.nextInt(), sc.nextInt());
  		Arrays.sort(array);
  		
  		int N = 1;
  		while(N < m)
  			N <<= 1;
  		SegmentTree tree = new SegmentTree(new int[N + 1]);
  		
  		int i = 0, j;
  		int min = Integer.MAX_VALUE;
  		for(j = 0; j < n; j++) {
  			tree.update_range(array[j].u + 1, array[j].v, 1);
  			int query = tree.query(2, m);
  			if(query > 0)
  				while(i <= j) {
  					min = Math.min(min, array[j].w - array[i].w);
  					tree.update_range(array[i].u + 1, array[i].v, -1);
  					i++;
  					query = tree.query(2, m);
  					if(query == 0)
  						break;
  				}
  		}
  		
  		pw.println(min);
  		pw.flush();
  	}
  	
  	public static class Tri implements Comparable<Tri> {
  		int u, v, w;
  		
  		public Tri(int u, int v, int w) {
  			this.u = u;
  			this.v = v;
  			this.w = w;
  		}
  		
  		public int compareTo(Tri t) {
  			return this.w - t.w;
  		}
  	}
  	
  	public static class SegmentTree {
  		
  		int INF = (int)1e9;
  		int N;
  		int[] in, sTree, lazy;
  		
  		public SegmentTree(int[] array) {
  			in = array;
  			N = array.length - 1;
  			sTree = new int[N<<1];
  			lazy = new int[N<<1];
  			build(1, 1, N);
  		}
  		
  		public void build(int node, int b, int e) {
  			if(b == e) {
  				sTree[node] = in[b];
  				return;
  			}
  			
  			int left = node<<1, right = node<<1|1;
  			int mid = (b + e)>>1;
  			
  			build(left, b, mid);
  			build(right, mid + 1, e);
  			
  			sTree[node] = Math.min(sTree[left], sTree[right]);
  		}
  		
  		public int query(int l, int r) {
  			return query(1, 1, N, l, r);
  		}
  		
  		public int query(int node, int b, int e, int l, int r) {
  			if(r < b || l > e)
  				return INF;
  			if(b >= l && e <= r)
  				return sTree[node];
  			
  			int left = node<<1, right = node<<1|1;
  			int mid = (b + e)>>1;
  			
  			propagate(node, b, e, left, right, mid);
  			
  			return Math.min(query(left, b, mid, l, r), query(right, mid + 1, e, l, r));
  		}
  		
  		public void update_range(int l, int r, int v) {
  			update_range(1, 1, N, l, r, v);
  		}
  		
  		public void update_range(int node, int b, int e, int l, int r, int v) {
  			if(l > e || r < b)
  				return;
  			if(b >= l && e <= r) {
  				sTree[node] += v;
  				lazy[node] += v;
  				return;
  			}
  			
  			int left = node<<1, right = node<<1|1;
  			int mid = (b + e)>>1;
  			
  			propagate(node, b, e, left, right, mid);
  			
  			update_range(left, b, mid, l, r, v);
  			update_range(right, mid + 1, e, l, r, v);
  			
  			sTree[node] = Math.min(sTree[left], sTree[right]);
  		}
  		
  		public void propagate(int node, int b, int e, int left, int right, int mid) {
  			lazy[left] += lazy[node];
  			lazy[right] += lazy[node];
  			sTree[left] += lazy[node];
  			sTree[right] += lazy[node];
  			lazy[node] = 0;
  		}
  		
  	}
  
  	public static class Scanner {
  		StringTokenizer st;
  		BufferedReader br;
  
  		public Scanner(InputStream system) {
  			br = new BufferedReader(new InputStreamReader(system));
  		}
  
  		public Scanner(String file) throws Exception {
  			br = new BufferedReader(new FileReader(file));
  		}
  
  		public String next() throws IOException {
  			while (st == null || !st.hasMoreTokens())
  				st = new StringTokenizer(br.readLine());
  			return st.nextToken();
  		}
  
  		public String nextLine() throws IOException {
  			return br.readLine();
  		}
  
  		public int nextInt() throws IOException {
  			return Integer.parseInt(next());
  		}
  
  		public double nextDouble() throws IOException {
  			return Double.parseDouble(next());
  		}
  
  		public char nextChar() throws IOException {
  			return next().charAt(0);
  		}
  
  		public long nextLong() throws IOException {
  			return Long.parseLong(next());
  		}
  
  		public int[] nextIntArray(int n) throws IOException {
  			int[] array = new int[n];
  			for (int i = 0; i < n; i++)
  				array[i] = nextInt();
  			return array;
  		}
  
  		public Integer[] nextIntegerArray(int n) throws IOException {
  			Integer[] array = new Integer[n];
  			for (int i = 0; i < n; i++)
  				array[i] = new Integer(nextInt());
  			return array;
  		}
  
  		public long[] nextLongArray(int n) throws IOException {
  			long[] array = new long[n];
  			for (int i = 0; i < n; i++)
  				array[i] = nextLong();
  			return array;
  		}
  
  		public double[] nextDoubleArray(int n) throws IOException {
  			double[] array = new double[n];
  			for (int i = 0; i < n; i++)
  				array[i] = nextDouble();
  			return array;
  		}
  
  		public static int[] shuffle(int[] a) {
  			int n = a.length;
  			Random rand = new Random();
  			for (int i = 0; i < n; i++) {
  				int tmpIdx = rand.nextInt(n);
  				int tmp = a[i];
  				a[i] = a[tmpIdx];
  				a[tmpIdx] = tmp;
  			}
  			return a;
  		}
  
  		public boolean ready() throws IOException {
  			return br.ready();
  		}
  
  		public void waitForInput() throws InterruptedException {
  			Thread.sleep(3000);
  		}
  	}
  }
  