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 codeforces {
  	public static void main(String[] args) throws Exception {
  
  		int n=sc.nextInt();
  		int m=sc.nextInt();
  		Integer[][]a=new Integer[n][3];
  		for(int i=0;i<n;i++) {
  			a[i][0]=sc.nextInt();
  			a[i][1]=sc.nextInt();
  			a[i][2]=sc.nextInt();
  		}
  		Arrays.sort(a,(x,y)->x[2]-y[2]);
  		
  		SegmentTree sg=new SegmentTree(m);
  		long ans=Long.MAX_VALUE;
  		int j=0;
  		for(int i=0;i<n;i++) {
  			if(j<=i) {
  				j=i+1;
  				sg.update_range(a[i][0]-1, a[i][1]-2, 1);
  				
  			}
  			while(j<n&&sg.query(0, m-2)==0) {
  				sg.update_range(a[j][0]-1, a[j][1]-2, 1);
  				j++;
  			}
  			if(sg.query(0, m-2)!=0) {
  //				pw.println(a[j-1][2]+" "+a[i][2]);
  				ans=Math.min(ans, a[j-1][2]-a[i][2]);
  			}
  			sg.update_range(a[i][0]-1, a[i][1]-2, -1);
  			
  		}
  		pw.println(ans);
  		
  		
  		pw.close();
  	}
  	public static class SegmentTree { // 1-based DS, OOP
  
  		int N; // the number of elements in the array as a power of 2 (i.e. after padding)
  		long[] array;
  		long[] sTree;
  		long[] lazy;
  
  		SegmentTree(int a) {
  
  			int nn = a;
  			int NN = 1;
  			while (NN < nn)
  				NN <<= 1; // padding
  
  			long[] in = new long[NN + 1];
  			
  
  			array = in;
  			N = in.length - 1;
  			sTree = new long[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
  			lazy = new long[N << 1];
  			build(1, 1, N);
  		}
  		SegmentTree(long[] a) {
  
  			int nn = a.length;
  			int NN = 1;
  			while (NN < nn)
  				NN <<= 1; // padding
  
  			long[] in = new long[NN + 1];
  			for (int i = 1; i <= nn; i++)
  				in[i] = a[i - 1];
  
  			array = in;
  			N = in.length - 1;
  			sTree = new long[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
  			lazy = new long[N << 1];
  			build(1, 1, N);
  		}
  
  		SegmentTree(int[] a) {
  
  			int nn = a.length;
  			int NN = 1;
  			while (NN < nn)
  				NN <<= 1; // padding
  
  			long[] in = new long[NN + 1];
  
  			for (int i = 1; i <= nn; i++)
  				in[i] = a[i - 1];
  
  			array = in;
  			N = in.length - 1;
  			sTree = new long[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
  
  			lazy = new long[N << 1];
  
  			build(1, 1, N);
  		}
  
  		void build(int node, int b, int e)	// O(n)
  		{
  			if(b == e)					
  				sTree[node] = array[b];
  			else						
  			{
  				int mid = b + e >> 1;
  				build(node<<1,b,mid);
  				build(node<<1|1,mid+1,e);
  				sTree[node] = Math.min(sTree[node<<1],sTree[node<<1|1]);
  			}
  		}
  		
  		
  		void update_point(int index, int val)			// O(log n)
  		{
  			index += N ;				
  			sTree[index] += val;			
  			while(index>1)				
  			{
  				index >>= 1;
  				sTree[index] = Math.min(sTree[index<<1] , sTree[index<<1|1]);		
  			}
  		}
  		
  		
  		void update_range(int i, int j, int val)		// O(log n) 
  		{
  			update_range(1,1,N,i+1,j+1,val);
  		}
  		
  		void update_range(int node, int b, int e, int i, int j, int val)
  		{
  			if(i > e || j < b)		
  				return;
  			if(b >= i && e <= j)		
  			{
  				sTree[node] += val;			
  				lazy[node] += val;				
  			}							
  			else		
  			{
  				int mid = b + e >> 1;
  				propagate(node, b, mid, e);
  				update_range(node<<1,b,mid,i,j,val);
  				update_range(node<<1|1,mid+1,e,i,j,val);
  				sTree[node] = Math.min(sTree[node<<1] , sTree[node<<1|1]);		
  			}
  			
  		}
  		void propagate(int node, int b, int mid, int e)		
  		{
  			lazy[node<<1] += lazy[node];
  			lazy[node<<1|1] += lazy[node];
  			sTree[node<<1] += lazy[node];		
  			sTree[node<<1|1] += lazy[node];		
  			lazy[node] = 0;
  		}
  		
  		long query(int i, int j)
  		{
  			return query(1,1,N,i+1,j+1);
  		}
  		
  		long query(int node, int b, int e, int i, int j)	// O(log n)
  		{
  			if(i>e || j <b)
  				return Long.MAX_VALUE;		
  			if(b>= i && e <= j)
  				return sTree[node];
  			int mid = b + e >> 1;
  			propagate(node, b, mid, e);
  			long q1 = query(node<<1,b,mid,i,j);
  			long q2 = query(node<<1|1,mid+1,e,i,j);
  			return Math.min(q1 , q2);	
  					
  		}
  
  	}
  
  	static class Scanner {
  		StringTokenizer st;
  		BufferedReader br;
  
  		public Scanner(InputStream s) {
  			br = new BufferedReader(new InputStreamReader(s));
  		}
  
  		public Scanner(FileReader r) {
  			br = new BufferedReader(r);
  		}
  
  		public String next() throws IOException {
  			while (st == null || !st.hasMoreTokens())
  				st = new StringTokenizer(br.readLine());
  			return st.nextToken();
  		}
  
  		public int nextInt() throws IOException {
  			return Integer.parseInt(next());
  		}
  
  		public long nextLong() throws IOException {
  			return Long.parseLong(next());
  		}
  
  		public String nextLine() throws IOException {
  			return br.readLine();
  		}
  
  		public double nextDouble() throws IOException {
  			String x = next();
  			StringBuilder sb = new StringBuilder("0");
  			double res = 0, f = 1;
  			boolean dec = false, neg = false;
  			int start = 0;
  			if (x.charAt(0) == '-') {
  				neg = true;
  				start++;
  			}
  			for (int i = start; i < x.length(); i++)
  				if (x.charAt(i) == '.') {
  					res = Long.parseLong(sb.toString());
  					sb = new StringBuilder("0");
  					dec = true;
  				} else {
  					sb.append(x.charAt(i));
  					if (dec)
  						f *= 10;
  				}
  			res += Long.parseLong(sb.toString()) / f;
  			return res * (neg ? -1 : 1);
  		}
  
  		public long[] nextlongArray(int n) throws IOException {
  			long[] a = new long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextLong();
  			return a;
  		}
  
  		public Long[] nextLongArray(int n) throws IOException {
  			Long[] a = new Long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextLong();
  			return a;
  		}
  
  		public int[] nextIntArray(int n) throws IOException {
  			int[] a = new int[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextInt();
  			return a;
  		}
  
  		public Integer[] nextIntegerArray(int n) throws IOException {
  			Integer[] a = new Integer[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextInt();
  			return a;
  		}
  
  		public boolean ready() throws IOException {
  			return br.ready();
  		}
  
  	}
  
  	static class pair implements Comparable<pair> {
  		long x;
  		long y;
  
  		public pair(long x, long y) {
  			this.x = x;
  			this.y = y;
  		}
  
  		public String toString() {
  			return x + " " + y;
  		}
  
  		public boolean equals(Object o) {
  			if (o instanceof pair) {
  				pair p = (pair) o;
  				return p.x == x && p.y == y;
  			}
  			return false;
  		}
  
  		public int hashCode() {
  			return new Long(x).hashCode() * 31 + new Long(y).hashCode();
  		}
  
  		public int compareTo(pair other) {
  			if (this.x == other.x) {
  				return Long.compare(this.y, other.y);
  			}
  			return Long.compare(this.x, other.x);
  		}
  	}
  
  	static class tuble implements Comparable<tuble> {
  		int x;
  		int y;
  		int z;
  
  		public tuble(int x, int y, int z) {
  			this.x = x;
  			this.y = y;
  			this.z = z;
  		}
  
  		public String toString() {
  			return x + " " + y + " " + z;
  		}
  
  		public int compareTo(tuble other) {
  			if (this.x == other.x) {
  				if (this.y == other.y) {
  					return this.z - other.z;
  				}
  				return this.y - other.y;
  			} else {
  				return this.x - other.x;
  			}
  		}
  	}
  
  	static long mod = 1000000007;
  	static Random rn = new Random();
  	static Scanner sc = new Scanner(System.in);
  	static PrintWriter pw = new PrintWriter(System.out);
  }