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 extends PrintWriter {
      
      void solve() {
          int n = sc.nextInt();
          int m = sc.nextInt();
          
          int[][] segments = new int[n][];
          for(int i = 0; i < n; i++) {
              segments[i] = new int[] {sc.nextInt(), sc.nextInt(), sc.nextInt()};
          }
          
          Arrays.sort(segments, Comparator.comparing(arr -> arr[2]));
          
          min = new int[4*m];
          old = new boolean[4*m];
          lazy = new int[4*m];
          
          long ans = 1L<<60;
          int l = 0;
          for(int r = 0; r < n; r++) {
              add_range(1, m, 1, segments[r][0], segments[r][1]-1, 1);
              while(query_range(1, m, 1, 1, m-1) > 0) {
                  ans = Math.min(ans, segments[r][2] - segments[l][2]);
                  add_range(1, m, 1, segments[l][0], segments[l][1]-1, -1);
                  l++;
              }
          }
          println(ans);
      }
      
      int[] min;
      
      boolean[] old;    
      int[] lazy;
      
      void add_range(int il, int ir, int i, int ql, int qr, int x) {
          lazy_propagate(i, il, ir);
          if(il > qr || ql > ir) return;
          
          if(ql <= il && ir <= qr) {
              lazy_update(i, x);
              lazy_propagate(i, il, ir);
              return;
          }
          
          int imid = (il+ir)/2;
          add_range(il, imid, 2*i, ql, qr, x);
          add_range(imid+1, ir, 2*i+1, ql, qr, x);      
          
          min[i] = Math.min(min[2*i], min[2*i+1]);
      }
      
      int query_range(int il, int ir, int i, int ql, int qr) {
          lazy_propagate(i, il, ir);
          if(il > qr || ql > ir) return 1<<30;
          
          if(ql <= il && ir <= qr) {
              return min[i];
          }
          
          int imid = (il+ir)/2;      
          
          return Math.min(
                  query_range(il, imid, 2*i, ql, qr),
                  query_range(imid+1, ir, 2*i+1, ql, qr));
      } 
      
      void lazy_update(int i, int x) {
          old[i] = true;
          lazy[i] += x;
      }
      
      void lazy_propagate(int i, int il, int ir) {
          if(old[i]) {
              min[i] += lazy[i];
              if(il != ir) {
                  lazy_update(2*i, lazy[i]);
                  lazy_update(2*i+1, lazy[i]);
              }
              lazy_reset(i);
          }
      }
      
      void lazy_reset(int i) {
          old[i] = false;
          lazy[i] = 0;
      }
      
          
      
  //  Main() throws FileNotFoundException { super(new File("output.txt")); }
  //  InputReader sc = new InputReader(new FileInputStream("1_08"));
    Main() { super(System.out); }
    InputReader sc = new InputReader(System.in);
    static class InputReader {
        InputReader(InputStream in) { this.in = in; } InputStream in;
        
        private byte[] buf = new byte[16384];
        private int    curChar;
        private int    numChars;
        
   
        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }
   
        private String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }
   
        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
   
        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
   
        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
   
        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
   
        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }
   
      public static void main(String[] $) {
          new Thread(null, new Runnable() {
              public void run() {
                  long start = System.nanoTime();
                  try {Main solution = new Main(); solution.solve(); solution.flush();} 
                  catch (Exception e) {e.printStackTrace(); System.exit(1);}
                  System.err.println((System.nanoTime()-start)/1E9);
              }
          }, "1", 1 << 27).start();
   
      }
  }