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 a {
      static int n,m;
      public static void main(String args[])
      {
          FastScanner fs=new FastScanner();
          n=fs.nextInt();
          m=fs.nextInt();
          ArrayList<ArrayList<Integer>>v=new ArrayList<>();
          for(int i=1;i<=n;i++)
          {
              int l,r,k;
              l=fs.nextInt();
              r=fs.nextInt();
              k=fs.nextInt();
              v.add(new ArrayList<Integer>(Arrays.asList(l,r,k)));
          }
          Collections.sort(v,new Comparator<ArrayList<Integer>>(){
              @Override
              public int compare(ArrayList<Integer>a,ArrayList<Integer>b)
              {
                  return a.get(2).compareTo(b.get(2));
              }
          });
          // for(ArrayList<Integer> it:v)
          //     System.out.println(it);
          segtree treeroot=new segtree();
          int ans=100000000;
          for(int i=0,j=0;i<n;i++)
          {
              while(j<n && treeroot.val==0)
              {
                  treeroot.update(1,m-1,v.get(j).get(0),v.get(j).get(1)-1,1);
                  j++;
              }
              // System.out.println(i+" "+j+" "+(v.get(j-1).get(2)-v.get(i).get(2)));
              if(treeroot.val>0)
                  ans=Math.min(ans,v.get(j-1).get(2)-v.get(i).get(2));
              treeroot.update(1,m-1,v.get(i).get(0),v.get(i).get(1)-1,-1);
          }
          System.out.println(ans);
      }
      public static class segtree{
      public segtree left=null,right=null;
      public int val=0,lazy=0;
      public void apply(int x)
      {
          val += x;
          lazy += x;
      }
      public void push()
      {
          if(left==null)
          {
              left=new segtree();
              right=new segtree();
          }
          if(lazy!=0)
          {
              left.apply(lazy);
              right.apply(lazy);
              lazy=0;
          }
      }
      public void pull()
      {
          val=Math.min(left.val,right.val);
      }
      public void update(int l,int r,int s,int e,int x)
      {
          if(s>r || e<l)
              return;
          if(s<=l && e>=r)
          {
              apply(x);
              return;
          }
          int mid=(l+r)/2;
          push();
          left.update(l,mid,s,e,x);
          right.update(mid+1,r,s,e,x);
          pull();
      }
  }
      static class FastScanner{
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer st=new StringTokenizer("");
          String next()
          {
              if(!st.hasMoreTokens())
              {
                  try{
                      st=new StringTokenizer(br.readLine());
                  }
                  catch(Exception e)
                  {
                      e.printStackTrace();
                  }
              }
              return st.nextToken();
          }
          int nextInt()
          {
              return Integer.parseInt(next());
          }
      }
  }