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 {
      final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      static StringTokenizer st;
  
      static int readInt() throws IOException {
          while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
          return Integer.parseInt(st.nextToken());
      }
  
      static class Segment implements Comparable<Segment> {
          int L, R, w;
  
          Segment(int L, int R, int w) {
              this.L = L;
              this.R = R;
              this.w = w;
          }
  
          public int compareTo(Segment other) {
              return this.w - other.w;
          }
      }
  
      static int t[], lz[];
  
      static void update(int qL, int qR, int val, int L, int R, int v) {
          if (qL <= L && R <= qR) {
              t[v] = Math.max(t[v], val);
              lz[v] = Math.max(lz[v], val);
          } else if (qL <= R && L <= qR) {
              int M = (L + R) >> 1, v2 = v << 1, v21 = v2 | 1;
              if (lz[v] != 0) {
                  t[v2] = Math.max(t[v2], lz[v]);
                  lz[v2] = Math.max(lz[v2], lz[v]);
                  t[v21] = Math.max(t[v21], lz[v]);
                  lz[v21] = Math.max(lz[v21], lz[v]);
                  lz[v] = 0;
              }
              update(qL, qR, val, L, M, v2);
              update(qL, qR, val, M + 1, R, v21);
              t[v] = Math.min(t[v2], t[v21]);
          }
      }
  
      public static void main(String[] args) throws IOException {
          final int n = readInt(), m = readInt();
          final Segment seg[] = new Segment[n + 1];
          for (int i = 1; i <= n; ++i) {
              seg[i] = new Segment(readInt(), readInt(), readInt());
              if (seg[i].L != 1) ++seg[i].L;
          }
          Arrays.sort(seg, 1, n + 1);
          t = new int[m << 2];
          lz = new int[m << 2];
          int res = Integer.MAX_VALUE;
          for (int i = 1; i <= n; ++i) {
              update(seg[i].L, seg[i].R, seg[i].w, 1, m, 1);
              if (t[1] >= 1) res = Math.min(res, seg[i].w - t[1]);
          }
          final PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
          pr.println(res);
          pr.close();
      }
  }