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.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.StringTokenizer;
  
  /*
   * 0 1 2 3 4 5
   * 01234567890
   */
  public class E {
  
  	public static void main(String[] args) {
  		FastScanner fs=new FastScanner();
  		int n=fs.nextInt();
  		int m=fs.nextInt();
  		ST st=new ST(0, (m-1)*2);
  		Seg[] segs=new Seg[n];
  		for (int i=0; i<n; i++) 
  			segs[i]=new Seg((fs.nextInt()-1)*2, (fs.nextInt()-1)*2, fs.nextInt());
  		Arrays.sort(segs);
  		
  		int l=0, r=-1;
  		long ans=Long.MAX_VALUE;
  		outer: while (l<n) {
  			while (st.min()==0) {
  				r++;
  				if (r>=n) break outer;
  				st.rangeAdd(segs[r].l, segs[r].r, 1);
  			}
  			//TODO: consider possible answer
  			ans=Math.min(ans, segs[r].cost-segs[l].cost);
  			
  			st.rangeAdd(segs[l].l, segs[l].r, -1);
  			l++;
  		}
  		System.out.println(ans);
  	}
  	
  	static class Seg implements Comparable<Seg> {
  		int l, r, cost;
  		public Seg(int l, int r, int cost) {
  			this.l=l;
  			this.r=r;
  			this.cost=cost;
  		}
  		public int compareTo(Seg o) {
  			return Integer.compare(cost, o.cost);
  		}
  	}
  
  	static class ST {
  		int leftmost, rightmost, toProp, min;
  		ST lChild, rChild;
  		
  		public ST(int l, int r) {
  			leftmost=l;
  			rightmost=r;
  			if (leftmost!=rightmost) {
  				int mid=(leftmost+rightmost)/2;
  				lChild=new ST(l, mid);
  				rChild=new ST(mid+1, r);
  			}
  		}
  		
  		public void recalc() {
  			if (lChild!=null)
  				min=Math.min(lChild.min(), rChild.min());
  		}
  		public void prop() {
  			if (leftmost!=rightmost) {
  				lChild.toProp+=toProp;
  				rChild.toProp+=toProp;
  				toProp=0;
  			}
  			recalc();
  		}
  		
  		void rangeAdd(int l, int r, int d) {
  			if (l>rightmost || r<leftmost) return;
  			if (l<=leftmost && r>=rightmost) {
  				toProp+=d;
  				return;
  			}
  			lChild.rangeAdd(l, r, d);
  			rChild.rangeAdd(l, r, d);
  			
  			recalc();
  		}
  		
  		int min() {
  			return min+toProp;
  		}
  		
  	}
  	
  	static void sort(int[] a) {
  		ArrayList<Integer> l=new ArrayList<>();
  		for (int i:a) l.add(i);
  		Collections.sort(l);
  		for (int i=0; i<a.length; i++) a[i]=l.get(i);
  	}
  	
  	static class FastScanner {
  		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  		StringTokenizer st=new StringTokenizer("");
  		String next() {
  			while (!st.hasMoreTokens())
  				try {
  					st=new StringTokenizer(br.readLine());
  				} catch (IOException e) {
  					e.printStackTrace();
  				}
  			return st.nextToken();
  		}
  		
  		int nextInt() {
  			return Integer.parseInt(next());
  		}
  		int[] readArray(int n) {
  			int[] a=new int[n];
  			for (int i=0; i<n; i++) a[i]=nextInt();
  			return a;
  		}
  		long nextLong() {
  			return Long.parseLong(next());
  		}
  	}
  
  	
  }
  