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: 					/*
  					TO LEARN
  					1-segment trees
  					2-euler tour
  				
  					*/
  					/*
  					TO SOLVE 
  					uva 1103
  					codeforces 722 kavi on pairing duty
  					*/
  					/*
  					bit manipulation shit
  					1-Computer Systems: A Programmer's Perspective
  					2-hacker's delight
  					3-(02-03-bits-ints)
  					4-machine-basics
  					5-Bits Manipulation tutorialspoint
  					*/
  					/*
  					TO WATCH
  					1-what is bitmasking by kartik arora youtube
  					*/
  					            import java.util.*;
  							    import java.math.*;
  							    import java.io.*; 
  							    import java.util.stream.Collectors;
  
  							       public class A{
  					            static FastScanner scan=new FastScanner();
  							     public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
  	static Pair arr[];
  	static int tree[];
  	static int lazy[];
  	
  			
  			static int get(int v, int tl, int tr, int l,int r) {
      if (tl>r||tr<l)
      	return Integer.MAX_VALUE;
      if(tl>=l&&tr<=r)
          return tree[v];
      int tm = (tl + tr) / 2;     
          return Math.min(get(v*2+2, tm+1, tr, l,r), get(v*2+1, tl, tm, l,r));
  }
  static void update(int start,int end,int l,int r,int dif,int node){
  	if(lazy[node]!=0){
  		// there are pending updates
  		tree[node]+=lazy[node];
  		if(start!=end){
  			lazy[2*node+1]+=lazy[node];
  			lazy[2*node+2]+=lazy[node];
  		}
  		lazy[node]=0;
  	}
  	if(start> r || end<l)return;
  	else if(start>=l && end<=r){
  		tree[node]+=dif;	// make the update for the current range and keep them pending for the child nodes
  		if(start!=end){
  			lazy[2*node+1]+=dif;
  			lazy[2*node+2]+=dif;
  		}
  		return;
  	}
  	int mid=(start+end)/2;
  	update(start,mid,l,r,dif,2*node+1);
  	update(mid+1,end,l,r,dif,2*node+2);
  	tree[node]=Math.min(tree[2*node+1],tree[2*node+2]);
  }
  static int queryMin(int start,int end,int l,int r,int node){
  	return tree[0];
  }
  				public static void main(String[] args)  throws Exception 
  					      {
  					 
  
  								///scan=new FastScanner("cownomics.in");
  							//out = new PrintWriter("cownomics.out");
  							 
  							 							 /*
  							 READING 
  						
  							 3-Introduction to DP with Bitmasking codefoces
  							 4-Bit Manipulation hackerearth
  							 5-read more about mobious and inculsion-exclusion 
  							 */
  		
  
  							    int tt=1;	  
  				//tt=scan.nextInt();
  						    
  						//	System.out.println(2^3);
  						 outer:while(tt-->0)
  							    {
  							   
  							    int n=scan.nextInt(),m=scan.nextInt();
  							  arr=new Pair[n];
  							  tree=new int[4*(m-1)*2];
  							  lazy=new int[4*(m-1)*2];
  							// update(0,(m-1)*2,2,4,1,0);
  							//  System.out.println(queryMin(0,(m-1)*2,2,4,0));
  							  for(int i=0;i<n;i++)
  							  	arr[i]=new Pair((scan.nextInt()-1)*2,(scan.nextInt()-1)*2,scan.nextInt());
  							
  							   int l=0,r=-1;
  							   Arrays.sort(arr);
  							    // out.println(Arrays.toString(arr));
  							   int res=Integer.MAX_VALUE;
  							  all: while(l<n)
  							   {
  							   	while(queryMin(0,(m-1)*2,0,(m-1)*2,0)==0)
  							   	{
  							   		r++;
  							   		if(r>=n){
  							   		//	out.println(l);
  							   			break all;
  							   		}
  							   		update(0,(m-1)*2,(int)arr[r].x,(int)arr[r].y,1,0);
  							   	}
  							   	res=Math.min(res,(int)(arr[r].z-arr[l].z));
  							   	update(0,(m-1)*2,(int)arr[l].x,(int)arr[l].y,-1,0);
  							   	l++;
  							   }
  							   out.println(res);
  							   
  							    	}
  
  							
  
  	out.close();
  				}
  	    
  							static class special{
  								boolean bool;
  								long n;
  								//int id;
  								special(boolean bool,long n)
  								{
  									this.bool=bool;
  									this.n=n;
  								}
  								@Override
  								    public int hashCode() {
  							                int hash = 7;
  				    hash = 31 * hash + (int) n;
  				   
  				    return hash;
  							            }
  							              @Override
  							            public boolean equals(Object o){
  							                if (o == this) return true;
  							                if (o.getClass() != getClass()) return false;
  							                special t = (special)o;
  							                return t.bool == bool && t.n == n;
  							            }
  							}
  
  							   
  							     static long binexp(long a,long n)
  							    {
  							        if(n==0)
  							            return 1;
  							        long res=binexp(a,n/2);
  							        if(n%2==1)
  							            return res*res*a;
  							        else 
  							            return res*res;
  							    }
  							     
  							    static long powMod(long base, long exp, long mod) {
  							           if (base == 0 || base == 1) return base;
  							           if (exp == 0) return 1;
  							           if (exp == 1) return (base % mod+mod)%mod;
  							           long R = (powMod(base, exp/2, mod) % mod+mod)%mod;
  							           R *= R;
  							           R %= mod;
  							           if ((exp & 1) == 1) {
  							               return (base * R % mod+mod)%mod;
  							           }
  							           else return (R %mod+mod)%mod;
  							       }
  							    static double dis(double x1,double y1,double x2,double y2)
  							    {
  							        return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  							    }
  							    static long mod(long x,long y)
  							        {
  							            if(x<0)
  							                x=x+(-x/y+1)*y;
  							            return x%y;
  							        }
  							     public static  long pow(long b, long e) {
  							                long r = 1;
  							                while (e > 0) {
  							                    if (e % 2 == 1) r = r * b ;
  							                    b = b * b;
  							                    e >>= 1;
  							                }
  							                return r;
  							            }
  							    private static void sort(long[] arr) {
  							            List<Long> list = new ArrayList<>();
  							            for (long object : arr) list.add(object);
  							            Collections.sort(list);
  							        //Collections.reverse(list);
  							            for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
  							        }
  							    private static void sort2(long[] arr) {
  							            List<Long> list = new ArrayList<>();
  							            for (Long object : arr) list.add(object);
  							            Collections.sort(list);
  							        Collections.reverse(list);
  							            for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
  							        }
  							     static class FastScanner
  					{
  					    private int BS = 1 << 16;
  					    private char NC = (char) 0;
  					    private byte[] buf = new byte[BS];
  					    private int bId = 0, size = 0;
  					    private char c = NC;
  					    private double cnt = 1;
  					    private BufferedInputStream in;
  					 
  					    public FastScanner() {
  					        in = new BufferedInputStream(System.in, BS);
  					    }
  					 
  					    public FastScanner(String s) {
  					        try {
  					            in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
  					        } catch (Exception e) {
  					            in = new BufferedInputStream(System.in, BS);
  					        }
  					    }
  					 
  					    private char getChar() {
  					        while (bId == size) {
  					            try {
  					                size = in.read(buf);
  					            } catch (Exception e) {
  					                return NC;
  					            }
  					            if (size == -1) return NC;
  					            bId = 0;
  					        }
  					        return (char) buf[bId++];
  					    }
  					 
  					    public int nextInt() {
  					        return (int) nextLong();
  					    }
  					 
  					    public int[] nextInts(int N) {
  					        int[] res = new int[N];
  					        for (int i = 0; i < N; i++) {
  					            res[i] = (int) nextLong();
  					        }
  					        return res;
  					    }
  					 
  					    public long[] nextLongs(int N) {
  					        long[] res = new long[N];
  					        for (int i = 0; i < N; i++) {
  					            res[i] = nextLong();
  					        }
  					        return res;
  					    }
  					 
  					    public long nextLong() {
  					        cnt = 1;
  					        boolean neg = false;
  					        if (c == NC) c = getChar();
  					        for (; (c < '0' || c > '9'); c = getChar()) {
  					            if (c == '-') neg = true;
  					        }
  					        long res = 0;
  					        for (; c >= '0' && c <= '9'; c = getChar()) {
  					            res = (res << 3) + (res << 1) + c - '0';
  					            cnt *= 10;
  					        }
  					        return neg ? -res : res;
  					    }
  					 
  					    public double nextDouble() {
  					        double cur = nextLong();
  					        return c != '.' ? cur : cur + nextLong() / cnt;
  					    }
  					 
  					    public double[] nextDoubles(int N) {
  					        double[] res = new double[N];
  					        for (int i = 0; i < N; i++) {
  					            res[i] = nextDouble();
  					        }
  					        return res;
  					    }
  					 
  					    public String next() {
  					        StringBuilder res = new StringBuilder();
  					        while (c <= 32) c = getChar();
  					        while (c > 32) {
  					            res.append(c);
  					            c = getChar();
  					        }
  					        return res.toString();
  					    }
  					 
  					    public String nextLine() {
  					        StringBuilder res = new StringBuilder();
  					        while (c <= 32) c = getChar();
  					        while (c != '\n') {
  					            res.append(c);
  					            c = getChar();
  					        }
  					        return res.toString();
  					    }
  					 
  					    public boolean hasNext() {
  					        if (c > 32) return true;
  					        while (true) {
  					            c = getChar();
  					            if (c == NC) return false;
  					            else if (c > 32) return true;
  					        }
  					    }
  					}
  
  							     static class Pair implements Comparable<Pair>{
  							            public long x, y,z;
  							            public Pair(long x1, long y1,long z1) {
  							                x=x1;
  							                y=y1;
  							                z=z1;
  							            }
  							             public Pair(long x1, long y1) {
  							                x=x1;
  							                y=y1;
  							    
  							            }
  							            
  							            @Override
  							            public int hashCode() {
  							                return (int)(x + 31 * y);
  							            }
  							            public String toString() {
  							                return x + " " + y+" "+z;
  							            }
  							            @Override
  							            public boolean equals(Object o){
  							                if (o == this) return true;
  							                if (o.getClass() != getClass()) return false;
  							                Pair t = (Pair)o;
  							                return t.x == x && t.y == y&&t.z==z;
  							            }
  							    public int compareTo(Pair o)
  							    {
  							    	
  							   
  							    	return (int)(z-o.z);
  							     
  							        }
  							     
  							    }
  							    
  							    }
  
  							     
  