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.lang.*;
  import java.util.*;
  import java.io.*;
  import java.math.*;
  
  public class Main {
      static class SegmentTree {
          long[] val;
          long[] lazy;
          SegmentTree(int n) {
              val = new long[4*n];
              lazy = new long[4*n];
              Arrays.fill(val,0);
          }
          //low/high 是当前区间  l/r 查询区间 
          long update(int treeIndex,int low,int high,int l,int r,int m) {
  //             System.out.println(String.format("update %d,%d,%d,%d,%d,%d",treeIndex,low,high,l,r,m));
  //             System.out.println(String.format("%d,%d,%d",treeIndex,val[treeIndex],lazy[treeIndex]));
              if(r<low || l>high) {
                  return 0;
              }
              if(l<=low && r>=high) {
                  val[treeIndex] += m;
                  lazy[treeIndex] += m;
                  return val[treeIndex];
              }
              if(lazy[treeIndex] != 0) {
                  lazy[treeIndex*2+1] += lazy[treeIndex];
                  lazy[treeIndex*2+2] += lazy[treeIndex];
                  val[treeIndex*2+1] += lazy[treeIndex];
                  val[treeIndex*2+2] += lazy[treeIndex];
                  lazy[treeIndex] = 0;
              }
              
              if (low != high) {
                  int mid = low+(high-low)/2;
                  if (r <= mid) {
                      update(treeIndex*2+1,low,mid,l,r,m);
                  } else if(l>mid) {
                      update(treeIndex*2+2,mid+1,high,l,r,m);
                  } else {
                      update(treeIndex*2+1,low,mid,l,r,m);
                      update(treeIndex*2+2,mid+1,high,l,r,m);
                  }
                  val[treeIndex] = Math.min(val[treeIndex*2+1],val[treeIndex*2+2]);
              }
              return val[treeIndex];
          }
          //low/high 是当前区间  l/r 查询区间 
          // l 区间起点 r 区间终点
          // res[0] mul res[1] add
          long query(int treeIndex,int low,int high,int l,int r) {
  //             System.out.println(String.format("query %d,%d,%d,%d,%d",treeIndex,low,high,l,r));
  //             System.out.println(String.format("%d,%d,%d",treeIndex,val[treeIndex],lazy[treeIndex]));
              long[] res = new long[2];
              if (l>high || r<low) {
                  return 0L;
              }
              if (l<=low && high<=r) {
                  return val[treeIndex];
              }
  
              if(lazy[treeIndex] != 0) {
                  lazy[treeIndex*2+1] += lazy[treeIndex];
                  lazy[treeIndex*2+2] += lazy[treeIndex];
                  val[treeIndex*2+1] += lazy[treeIndex];
                  val[treeIndex*2+2] += lazy[treeIndex];
                  lazy[treeIndex] = 0;
              }
              
  
              int mid = low+(high-low)/2;
              long res1 = query(treeIndex*2+1,low,mid,l,r);
              long res2 = query(treeIndex*2+2,mid+1,high,l,r);
              return Math.min(res1,res2);
          }
      }
      
      static void deal(int n,int m,int[][] arr) {
          Arrays.sort(arr,(o1,o2)->Integer.compare(o1[2],o2[2]));
          SegmentTree st = new SegmentTree(m-1);
          int start=0,end=0;
          int min = Integer.MAX_VALUE;
          while(end<=n) {
              long v = st.query(0, 1, m-1, 1, m-1);
              if(v>0) {
  //                System.out.println(String.format("v>=0 %d %d",start,end));
                  min = Math.min(min,arr[end-1][2]-arr[start][2]);
                  st.update(0,1,m-1,arr[start][0],arr[start][1]-1,-1);
                  start++;
              } else {
                  if(end == n) break;
  //                System.out.println(String.format("v==0 %d %d",start,end));
                  st.update(0,1,m-1,arr[end][0],arr[end][1]-1,1);
  //                System.out.println(Arrays.toString(st.val));
  //                System.out.println(Arrays.toString(st.lazy));
                  end++;
              }
  //            System.out.println(String.format("sem %d %d %d",start,end,min));
          }
          out.println(min);
      }
  
  	public static void main(String[] args) {
          MyScanner sc = new MyScanner();
          out = new PrintWriter(new BufferedOutputStream(System.out));
          int n = sc.nextInt();
          int m = sc.nextInt();
          int[][] arr = new int[n][3];
          for(int j=0;j<n;j++) {
              arr[j][0] = sc.nextInt();
              arr[j][1] = sc.nextInt();
              arr[j][2] = sc.nextInt();
          }
          deal(n,m,arr);
          out.close();
      }
      
      //-----------PrintWriter for faster output---------------------------------
      public static PrintWriter out;
      
      //-----------MyScanner class for faster input----------
      public static class MyScanner {
          BufferedReader br;
          StringTokenizer st;
          
          public MyScanner() {
                  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;
          }
          
      }
  }