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.ArrayList;
  import java.util.Arrays;
  import java.util.StringTokenizer;
  import java.util.List;
   import java.util.*;
   public class realfast implements Runnable 
    {
      private static final int INF = (int) 1e9;
      long in= 1000000007;
      long fac[]= new long[1000001];
      long inv[]=new long[1000001];
      int seg[]=new int[4*(1000000)+4];
      int lazy[]=new int[4*(1000000)+4];
      public  void solve() throws IOException 
      {
          
  
           int n = readInt();
           int m = readInt();
           ArrayList<edge> arr[]=new ArrayList[1000001];
           for(int i=1;i<=1000000;i++)
              arr[i]= new ArrayList<edge>();
           for(int i =0;i<n;i++)
           {
               int l = readInt();
               int r = readInt();
               int w = readInt();
               arr[w].add(new edge(l,r));
           }
           int ans=1000000;
           
           for(int i=1;i<=1000000;i++)
           {
               if(arr[i].size()>0)
               {
                  for(int j =0;j<arr[i].size();j++)
                  {
                       int u = arr[i].get(j).u;
                       int v = arr[i].get(j).v;
                       patch(1,m-1,0,u,v-1,i);
                  }
                  if(seg[0]!=0)
                  {
                      ans = Math.min(ans,i-seg[0]);
                  }
               }
           }
  
           out.println(ans);
      }
  
     
      public void patch( int left , int right,int index, int l , int r , int val)
      {
          if(left>right)
              return ;
         // System.out.println("yo");
          if(lazy[index]!=0)
          {
               seg[index]= lazy[index];
               lazy[index]=0;
               if(left!=right)
               {
                  lazy[2*index+1]=seg[index];
                  lazy[2*index+2]=seg[index];
               }
             
          }
  
          if(right<l||r<left)
          {
               return ;
          }
          if(left>=l&&right<=r)
          {
               seg[index]= val;
               if(left!=right)
               {
                  lazy[2*index+1]=val;
                  lazy[2*index+2]=val;
               }
               return ;
          }
  
          int mid = left+(right-left)/2;
          patch(left, mid,2*index+1,l,r,val);
          patch(mid+1,right,2*index+2,l,r,val);
          seg[index]=Math.min(seg[2*index+1],seg[2*index+2]);              
      }
      
      public int value (int seg[], int left , int right ,int index, int l, int r)
      {
              
              if(left>right)
              {
                return -100000000;
              }
              if(right<l||left>r)
                  return -100000000;
              if(left>=l&&right<=r)
                  return seg[index];
              int mid = left+(right-left)/2;
              int val = value(seg,left,mid,2*index+1,l,r);
              int val2 = value(seg,mid+1,right,2*index+2,l,r);
              return Math.max(val,val2);
  
      }
     
      public int gcd(int a , int b )
      {
        if(a<b)
        {
          int t =a;
          a=b;
          b=t;
        }
        if(a%b==0)
          return b ;
        return gcd(b,a%b);
      }
      public long pow(long n , long p,long m)
      {
           if(p==0)
              return 1;
          long val = pow(n,p/2,m);;
          val= (val*val)%m;
          if(p%2==0)
              return val;
          else
              return (val*n)%m;
      }
      
      
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      public static void main(String[] args) {
          new Thread(null, new realfast(), "", 128 * (1L << 20)).start();
      }
   
      private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
      private BufferedReader reader;
      private StringTokenizer tokenizer;
      private PrintWriter out;
   
      @Override
      public void run() {
          try {
              if (ONLINE_JUDGE || !new File("input.txt").exists()) {
                  reader = new BufferedReader(new InputStreamReader(System.in));
                  out = new PrintWriter(System.out);
              } else {
                  reader = new BufferedReader(new FileReader("input.txt"));
                  out = new PrintWriter("output.txt");
              }
              solve();
          } catch (IOException e) {
              throw new RuntimeException(e);
          } finally {
              try {
                  reader.close();
              } catch (IOException e) {
                  // nothing
              }
              out.close();
          }
      }
   
      private String readString() throws IOException {
          while (tokenizer == null || !tokenizer.hasMoreTokens()) {
              tokenizer = new StringTokenizer(reader.readLine());
          }
          return tokenizer.nextToken();
      }
   
      @SuppressWarnings("unused")
      private int readInt() throws IOException {
          return Integer.parseInt(readString());
      }
   
      @SuppressWarnings("unused")
      private long readLong() throws IOException {
          return Long.parseLong(readString());
      }
   
      @SuppressWarnings("unused")
      private double readDouble() throws IOException {
          return Double.parseDouble(readString());
      }
  }
  class edge implements Comparable<edge>{
      int u ;
      int v;
      
      edge(int  u, int v)
      {
         this.u=u;
         this.v=v;
      }
      public int compareTo(edge e)
      {
          return this.v-e.v;
      }
  }