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.*;
  import java.util.*;
  public class Main {
      public static void main(String args[])
      {
          FastReader input=new FastReader();
          PrintWriter out=new PrintWriter(System.out);
          int T=1;
          while(T-->0)
          {
              int n=input.nextInt();
              int m=input.nextInt();
              ArrayList<Pair> list=new ArrayList<>();
              for(int i=0;i<n;i++)
              {
                  int l=input.nextInt();
                  int r=input.nextInt();
                  int w=input.nextInt();
                  list.add(new Pair(l,r,w));
              }
              Collections.sort(list,new Comparator<Pair>(){
                  @Override
                  public int compare(Pair o1,Pair o2)
                  {
                      return o1.w-o2.w;
                  }
              });
  //            for(int i=0;i<list.size();i++)
  //            {
  //                out.println(list.get(i).w+" "+list.get(i).l+" "+list.get(i).r);
  //            }
              int a[]=new int[m+1];
              int seg[]=new int[4*m+1];
              int lazy[]=new int[4*m+1];
              buildSeg(a,seg,1,m,1);
              int x=-1;
              for(int i=0;i<list.size();i++)
              {
                  int l=list.get(i).l;
                  int r=list.get(i).r;
                  updateSegQuery(seg,lazy,l,r-1,1,1,m,1);
                  int min=rangeMinQuery(seg,lazy,1,m-1,1,m,1);
                  if(min>=1)
                  {
                      x=i;
                      break;
                  }
              }
              int min=Integer.MAX_VALUE;
              int in=0;
              while(in<list.size())
              {
                  int dif=list.get(x).w-list.get(in).w;
                  min=Math.min(min,dif);
                  updateSegQuery(seg,lazy,list.get(in).l,list.get(in).r-1,-1,1,m,1);
                  in++;
                  int f=0;
                  while(true)
                  {
                      int min1=rangeMinQuery(seg,lazy,1,m-1,1,m,1);
                      if(min1>=1)
                      {
                          f=1;
                          break;
                      }
                      x++;
                      if(x>=list.size())
                      {
                          break;
                      }
                      updateSegQuery(seg,lazy,list.get(x).l,list.get(x).r-1,1,1,m,1);
                  }
                  if(f==0)
                  {
                      break;
                  }
              }
              out.println(min);
          }
          out.close();
      }
      public static void updateSegQuery(int seg[],int lazy[],int ql,int qh,int d,int l,int h,int pos)
      {
          if(l>h)
          {
              return;
          }
          else
          {
              if(lazy[pos]!=0)
              {
                  seg[pos]+=lazy[pos];
                  if(l!=h)
                  {
                      lazy[pos*2]+=lazy[pos];
                      lazy[pos*2+1]+=lazy[pos];
                  }
                  lazy[pos]=0;
              }
              if(qh<l || ql>h)
              {
                  return;
              }
              else if(l>=ql && h<=qh)
              {
                  seg[pos]+=d;
                  if(l!=h)
                  {
                      lazy[pos*2]+=d;
                      lazy[pos*2+1]+=d;
                  }
                  return;
              }
              else
              {
                  int mid=(l+h)/2;
                  updateSegQuery(seg,lazy,ql,qh,d,l,mid,2*pos);
                  updateSegQuery(seg,lazy,ql,qh,d,mid+1,h,2*pos+1);
                  seg[pos]=Math.min(seg[2*pos],seg[2*pos+1]);
              }
          }
      }
      public static int rangeMinQuery(int seg[],int lazy[],int ql,int qh,int l,int h,int pos)
      {
          if(l>h)
          {
              return Integer.MAX_VALUE;
          }
          else
          {
              if(lazy[pos]!=0)
              {
                  seg[pos]+=lazy[pos];
                  if(l!=h)
                  {
                      lazy[pos*2]+=lazy[pos];
                      lazy[pos*2+1]+=lazy[pos];
                  }
                  lazy[pos]=0;
              }
              if(h<ql || l>qh)
              {
                  return Integer.MAX_VALUE;
              }
              else if(l>=ql && h<=qh)
              {
                  return seg[pos];
              }
              else
              {
                  int mid=(l+h)/2;
                  return Math.min(rangeMinQuery(seg,lazy,ql,qh,l,mid,2*pos),
                          rangeMinQuery(seg,lazy,ql,qh,mid+1,h,2*pos+1));
              }
          }
      }
      public static void buildSeg(int a[],int seg[],int l,int h,int pos)
      {
          if(l==h)
          {
              seg[pos]=a[l];
          }
          else
          {
              int mid=(l+h)/2;
              buildSeg(a,seg,l,mid,2*pos);
              buildSeg(a,seg,mid+1,h,2*pos+1);
              seg[pos]=Math.min(seg[2*pos],seg[2*pos+1]);
          }
      }
      public static class Pair
      {
          int l,r,w;
          Pair(int l,int r,int w)
          {
              this.l=l;
              this.r=r;
              this.w=w;
          }
      }
      static class FastReader
      {
          BufferedReader br;
          StringTokenizer st;
          public FastReader()
          {
              br = new BufferedReader(new InputStreamReader(System.in));
          }
          String next()
          {
              while (st == null || !st.hasMoreElements())
              {
                  try
                  {
                      st = new StringTokenizer(br.readLine());
                  }
                  catch (IOException e)
                  {
                      e.printStackTrace();
                  }
              }
              return st.nextToken();
          }
          int nextInt()
          {
              return Integer.parseInt(next());
          }
          long nextLong()
          {
              return Long.parseLong(next());
          }
          double nextDouble()
          {
              return Double.parseDouble(next());
          }
          String nextLine()
          {
              String str="";
              try
              {
                  str=br.readLine();
              }
              catch (IOException e)
              {
                  e.printStackTrace();
              }
              return str;
          }
      }
  }