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.*;
  import java.io.*;
  
  public class _112 {
      public static void main(String[] args) {
          MyScanner sc = new MyScanner();
          PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
          int t = 1;
          while (t-- > 0) {
              int n = sc.nextInt();
              int m = sc.nextInt();
              ArrayList<Seg> a = new ArrayList<>();
              for (int i = 0; i < n; i++) {
                  a.add(new Seg(sc.nextInt(), sc.nextInt(), sc.nextLong()));
              }
              Collections.sort(a, Comparator.comparingLong(x -> x.w));
              int p = 0;
              RangeTree seg = new RangeTree(2 * (m + 1));
              long res = Long.MAX_VALUE;
              for (int i = 0; i < n; i++) {
                  int l = a.get(i).l; int r = a.get(i).r;
                  seg.update(2 * l, 2 * r, 1);
                  while (p < i) {
                      // try to remove p
                      int s = a.get(p).l; int e = a.get(p).r;
                      if (seg.minQuery(2 * s, 2 * e) >= 2) {
                          seg.update(2 * s,2 * e, -1);
                          ++p;
                      } else break;
                  }
                  if (seg.minQuery(2 * 1,2 *  m) > 0) {
                      res = Math.min(res, a.get(i).w - a.get(p).w);
                  }
              }
              out.println(res);
          }
          out.close();
      }
  
  
      static class Seg {
          int l; int r;
          long w;
          Seg(int l, int r, long w) {
              this.l = l; this.r = r; this.w = w;
          }
      }
  
      static class RangeTree {
          private long[] min;
          private long[] lazy;
          private int size;
          public RangeTree(int size) {
              this.size = size;
              min = new long[4*size];
              lazy = new long[4*size];
          }
          public void update(int l, int r, int inc) {
              update(1, 0, size-1, l, r, inc);
          }
          private void pushDown(int index, int l, int r) {
              min[index] += lazy[index];
              if(l != r) {
                  lazy[2*index] += lazy[index];
                  lazy[2*index+1] += lazy[index];
              }
              lazy[index] = 0;
          }
          private void pullUp(int index, int l, int r) {
              int m = (l+r)/2;
              min[index] = Math.min(evaluateMin(2*index, l, m), evaluateMin(2*index+1, m+1, r));
          }
          private long evaluateMin(int index, int l, int r) {
              return min[index] + lazy[index];
          }
          private void update(int index, int l, int r, int left, int right, int inc) {
              if(r < left || l > right) return;
              if(l >= left && r <= right) {
                  lazy[index] += inc;
                  return;
              }
              pushDown(index, l, r);
              int m = (l+r)/2;
              update(2*index, l, m, left, right, inc);
              update(2*index+1, m+1, r, left, right, inc);
              pullUp(index, l, r);
          }
          public long minQuery(int l, int r) {
              return minQuery(1, 0, size-1, l, r);
          }
          private long minQuery(int index, int l, int r, int left, int right) {
              if(r < left || l > right) return Long.MAX_VALUE;
              if(l >= left && r <= right) {
                  return evaluateMin(index, l, r);
              }
              pushDown(index, l, r);
              int m = (l+r)/2;
              long ret = Long.MAX_VALUE;
              ret = Math.min(ret, minQuery(2*index, l, m, left, right));
              ret = Math.min(ret, minQuery(2*index+1, m+1, r, left, right));
              pullUp(index, l, r);
              return ret;
          }
  
      }
  
  
      static void sort(int[] a) {
          ArrayList<Integer> q = new ArrayList<>();
          for (int i : a) q.add(i);
          Collections.sort(q);
          for (int i = 0; i < a.length; i++) a[i] = q.get(i);
      }
  
      static void sort(long[] a) {
          ArrayList<Long> q = new ArrayList<>();
          for (long i : a) q.add(i);
          Collections.sort(q);
          for (int i = 0; i < a.length; i++) a[i] = q.get(i);
      }
  
  
      //-----------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;
          }
  
  
      }
  
  }