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.*;
  
  public class MyClass {
      static Scanner in= new Scanner(System.in);
      public static void main(String args[]) {
        int n= in.nextInt(), m= in.nextInt()-1;
        List<Triple> a= new ArrayList<>();
        for(int i=0;i<n;i++){
            int x,y,z;
            y= in.nextInt(); z= in.nextInt()-1; x= in.nextInt();
            a.add(new Triple(x,y,z));
        }
        Collections.sort(a);
        SEG seg= new SEG();
        int L=0;
        int ans= Integer.MAX_VALUE;
        for(int R=0;R<n;R++) {
            seg.update(1,1,m,a.get(R).y,a.get(R).z,1);
            while(true) {
              seg.update(1, 1, m, a.get(L).y, a.get(L).z, -1);
              if (seg.tree[1] > 0) {
                  L++;
                  continue;
              }
              seg.update(1, 1, m, a.get(L).y, a.get(L).z, 1);
              break;
            }
            if(seg.tree[1]>0) ans= Math.min(ans, a.get(R).x-a.get(L).x);
        }
        System.out.println(ans);
      }
      
  }
  
  class Triple implements Comparable<Triple>{
      public int x;
      public int y;
      public int z;
      public Triple(int x, int y, int z){
          this.x = x;this.y = y;this.z = z;
      }
      public int compareTo(Triple o){
          return this.x-o.x;
          
      }
  }
  
  class SEG {
      int MAXN= 1000100;
      public int[] tree= new int[4*MAXN], lazy= new int[4*MAXN];
      void push(int i, int L, int R){
          if(lazy[i]==0) return;
           tree[i] += lazy[i];
          if (L != R) {
              lazy[i << 1] += lazy[i];
              lazy[i << 1 | 1] += lazy[i];
          }
          lazy[i] = 0;
      }
      //update a[i]+= val for all i in [u,v]
      public void update(int i, int L, int R, int u, int v, int val) {
          push(i,L,R);
          if(L>v || R<u) return;
          if(L>=u && R<=v) {
              lazy[i] = val;
              push(i,L,R);
              return;
          }
          int mid= (L+R)>>1;
          update(i << 1, L, mid, u, v, val);
          update(i << 1 | 1, mid + 1, R, u, v, val);
          tree[i] = Math.min(tree[i << 1], tree[i << 1 | 1]);
      }
  }