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 Sol{
   
  /*   
  ->n=1 important test case
  ->Dont get stuck on one approach try all possibilities
  ->check int overflow,array bounds,etc
  ->Dont think just idea think of its (correct) implementation completely (then only code)
  */
   
  public static void main(String []args){
  //precomp();
  int times=1;while(times-->0){solve();}out.close();}
  //0 1 2 3 4 5
  //01234567890
  static void solve(){  
       int n=ni();int m=ni();
       Seg a[]=new Seg[n];
       for(int i=0;i<n;i++){a[i]=new Seg(2*(ni()-1),2*(ni()-1),ni());}
       Arrays.sort(a,(o1,o2)->o1.w-o2.w);
       m*=2;m--;
       int cm=m;
       while(Integer.bitCount(m)!=1)m++;
       
       st=new int[2*m];lazy=new int[2*m];
       for(int i=m+cm;i<2*m;i++){point_update(i,1);}
       int i=0;int j=0;int ans=Max;
       
       while(i<n){
        
        while(j<n && get()==0){
            
           update(1,0,m-1,a[j].l,a[j].r,1);
           //print();
            j++;
         }
         if(get()==0)break;
         
         ans=Math.min(ans,a[j-1].w-a[i].w);
           update(1,0,m-1,a[i].l,a[i].r,-1);
         i++;
       }
       out.println(ans);
       return;
   }
  
  static void print(){
     for(int i=0;i<st.length;i++){out.print(i%10+" ");}out.println();
     for(int i=0;i<st.length;i++){out.print(st[i]+" ");}out.println();
     for(int i=0;i<st.length;i++){out.print(lazy[i]+" ");}out.println();
   }        
  //-----------------Utility--------------------------------------------
  
  static class Seg{int l,r,w;
             public Seg(int l,int r,int w){
                  this.l=l;this.r=r;this.w=w;}
            }
  
  static int get(){return st[1]+lazy[1];}
  static int st[],lazy[];
  
  static void point_update(int i,int v){
      st[i]=v;int j=i/2;
      while(j>=1){
        st[j]=Math.min(st[2*j],st[2*j+1]);j/=2;
      }
   }
  static void prop(int i,int l,int r){
     if(l!=r)
     {lazy[2*i+1]+=lazy[i];lazy[2*i]+=lazy[i];}st[i]+=lazy[i];lazy[i]=0; 
   }
  
  static void update(int i,int l,int r,int L,int R,int v){
     prop(i,l,r);
     if(L>r || l>R)return;
     if(L<=l && R>=r){lazy[i]+=v;return;}
     int mid=(l+r)/2;
     update(2*i,l,mid,L,R,v);update(2*i+1,mid+1,r,L,R,v);
     //out.println(i+" "+l+" "+r+" "+L+" "+R);
     st[i]=Math.min(st[2*i]+lazy[2*i],st[2*i+1]+lazy[2*i+1]);
     
   }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  static long gcd(long a,long b){if(b==0)return a; return gcd(b,a%b);}
   
  static int Max=Integer.MAX_VALUE; static long mod=1000000007;
   
  static int v(char c){return (int)(c-'a');}
   
  public static long power(long x, long y )
      {
          //0^0 = 1
          long res = 1L;
          x = x%mod;
          while(y > 0)
          {
              if((y&1)==1)
                  res = (res*x)%mod;
              y >>= 1;
              x = (x*x)%mod;
          }
          return res;
      }
   
  static class Pair implements Comparable<Pair>{
          int id;int value;Pair next;
          public Pair(int id,int value) {
           
            this.id=id;this.value=value;next=null;
          }
          @Override
          public int compareTo(Pair p){return Long.compare(value,p.value);}
      }
   
  //----------------------I/O---------------------------------------------
   
  static InputStream inputStream = System.in;
  static OutputStream outputStream = System.out;
  static FastReader in=new FastReader(inputStream);
  static PrintWriter out=new PrintWriter(outputStream);
   
  static class FastReader 
  { 
    BufferedReader br; 
    StringTokenizer st; 
   
          FastReader(InputStream is) {
              br = new BufferedReader(new InputStreamReader(is));
          }
   
    public String next() 
    { 
        while (st == null || !st.hasMoreElements()) 
        { 
            try
            { 
                st = new StringTokenizer(br.readLine()); 
            } 
            catch (IOException  e) 
            { 
                e.printStackTrace(); 
            } 
        } 
        return st.nextToken(); 
    } 
   
    public int nextInt() 
    { 
        return Integer.parseInt(next()); 
    } 
   
    public long nextLong() 
    { 
        return Long.parseLong(next()); 
    } 
   
   public  double nextDouble() 
    { 
        return Double.parseDouble(next()); 
    } 
   
   
    String nextLine() 
    { 
        String str = ""; 
        try
        { 
            str = br.readLine(); 
        } 
        catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
        return str; 
    } 
  }
  static int ni() {
          return in.nextInt();
      }
  static long nl(){return in.nextLong();}
  static String ns(){return in.nextLine();}
  //---------------------I/O------------------------------------------------- 
  }