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.net.Inet4Address;
  import java.util.*;
  import java.io.*;
  
  public class Div735 {
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////                                                                                                               /////////
  ////////                                                                                                               /////////
  ////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM         OOOOOO             SSSSSSS      EEEEEEEEEEEEE  /////////
  ////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMMMM      MMMMMM      OOO      OOO        SSSS   SSS     EEEEEEEEEEEEE  /////////
  ////////   HHHH        HHHH  EEEEE           MMMM MMM  MMM MMMM    OOO          OOO    SSSS       SSS   EEEEE          /////////
  ////////   HHHH        HHHH  EEEEE           MMMM  MMMMMM  MMMM   OOO            OOO   SSSS             EEEEE          /////////
  ////////   HHHH        HHHH  EEEEE           MMMM          MMMM  OOO              OOO   SSSSSSS         EEEEE          /////////
  ////////   HHHHHHHHHHHHHHHH  EEEEEEEEEEE     MMMM          MMMM  OOO              OOO      SSSSSS       EEEEEEEEEEE    /////////
  ////////   HHHHHHHHHHHHHHHH  EEEEEEEEEEE     MMMM          MMMM  OOO              OOO         SSSSSSS   EEEEEEEEEEE    /////////
  ////////   HHHH        HHHH  EEEEE           MMMM          MMMM   OOO            OOO              SSSS  EEEEE          /////////
  ////////   HHHH        HHHH  EEEEE           MMMM          MMMM    OOO          OOO     SSS       SSSS  EEEEE          /////////
  ////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM      OOO      OOO        SSS    SSSS    EEEEEEEEEEEEE  /////////
  ////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM         OOOOOO             SSSSSSS      EEEEEEEEEEEEE  /////////
  ////////                                                                                                               /////////
  ////////                                                                                                               /////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      static int getcount(int v, int mod) {
          if (v < 0)
              return 0;
          int ans = v / 3;
          if (v % 3 >= mod)
              ans++;
          return ans;
      }
  
      public static void main(String[] args) throws IOException {
          Scanner sc = new Scanner(System.in);
          PrintWriter pw = new PrintWriter(System.out);
          int t = 1;
          while (t-- > 0) {
              int n = sc.nextInt();
              int m = sc.nextInt();
              int[][] a = new int[n][3];
  
              for (int i = 0; i < n; i++)
                  a[i] = sc.nextIntArr(3);
  
              int sz = 1;
              while (sz < m + 1) sz <<= 1;
              SegmentTree tree = new SegmentTree(sz);
              Arrays.sort(a, (x, y) -> x[2] - y[2]);
              int i = 0;
              int j = 0;
              tree.update_range(a[i][0] + 1, a[i][1], 1);
              tree.update_range(1, 1, 1);
              long ans = (long) 1e16;
  //            System.err.println(Arrays.deepToString(a));
              while (j < n - 1) {
                  while (tree.query(1, m) != 0) {
  //                    System.err.println(i + " " + j);
                      ans = Math.min(ans, a[j][2] - a[i][2]);
                      tree.update_range(a[i][0] + 1, a[i][1], -1);
                      i++;
                  }
                  while (j < n - 1 && tree.query(1, m) == 0) {
                      j++;
                      tree.update_range(a[j][0] + 1, a[j][1], 1);
                  }
              }
  
              while (tree.query(1, m) != 0) {
                  ans = Math.min(ans, a[j][2] - a[i][2]);
                  tree.update_range(a[i][0] + 1, a[i][1], -1);
                  i++;
              }
              pw.println(ans);
          }
          pw.flush();
      }
  
      static class SegmentTree {        // 1-based DS, OOP
  
          int N;            //the number of elements in the array as a power of 2 (i.e. after padding)
          long[] sTree, lazy;
  
          SegmentTree(int in) {
  
              N = in;
              sTree = new long[N << 1];        //no. of nodes = 2*N - 1, we add one to cross out index zero
              lazy = new long[N << 1];
  
          }
  
  
          void update_range(int i, int j, int val)        // O(log n)
          {
              update_range(1, 1, N, i, j, val);
          }
  
          void update_range(int node, int b, int e, int i, int j, int val) {
              if (i > e || j < b)
                  return;
              if (b >= i && e <= j) {
                  sTree[node] += val;
                  lazy[node] += val;
              } else {
                  int mid = b + e >> 1;
                  propagate(node, b, mid, e);
                  update_range(node << 1, b, mid, i, j, val);
                  update_range(node << 1 | 1, mid + 1, e, i, j, val);
                  sTree[node] = Math.min(sTree[node << 1], sTree[node << 1 | 1]);
              }
  
          }
  
          void propagate(int node, int b, int mid, int e) {
              lazy[node << 1] += lazy[node];
              lazy[node << 1 | 1] += lazy[node];
              sTree[node << 1] += lazy[node];
              sTree[node << 1 | 1] += lazy[node];
              lazy[node] = 0;
          }
  //        void propagate(int node, int b, int mid, int e) {
  //            lazy[node << 1] = Math.min(lazy[node], lazy[node << 1]);
  //            lazy[node << 1 | 1] = Math.min(lazy[node], lazy[node << 1 | 1]);
  //            sTree[node << 1] = Math.min(lazy[node], sTree[node << 1]);
  //            sTree[node << 1 | 1] += Math.min(lazy[node], sTree[node << 1 | 1]);
  //            lazy[node] = (long) 1e17;
  //        }
  
          long query(int i, int j) {
              return query(1, 1, N, i, j);
          }
  
          long query(int node, int b, int e, int i, int j)    // O(log n)
          {
              if (i > e || j < b)
                  return (long) 1e17;
              if (b >= i && e <= j)
                  return sTree[node];
              int mid = b + e >> 1;
              propagate(node, b, mid, e);
              long q1 = query(node << 1, b, mid, i, j);
              long q2 = query(node << 1 | 1, mid + 1, e, i, j);
              return Math.min(q1, q2);
  
          }
      }
  
      static class Scanner {
          StringTokenizer st;
          BufferedReader br;
  
          public Scanner(FileReader r) {
              br = new BufferedReader(r);
          }
  
          public Scanner(InputStream s) {
              br = new BufferedReader(new InputStreamReader(s));
          }
  
          public String next() throws IOException {
              while (st == null || !st.hasMoreTokens())
                  st = new StringTokenizer(br.readLine());
              return st.nextToken();
          }
  
          public int[] nextIntArr(int n) throws IOException {
              int[] a = new int[n];
              for (int i = 0; i < n; i++) {
                  a[i] = nextInt();
              }
              return a;
          }
  
          public int nextInt() throws IOException {
              return Integer.parseInt(next());
          }
  
          public long nextLong() throws IOException {
              return Long.parseLong(next());
          }
  
          public String nextLine() throws IOException {
              return br.readLine();
          }
  
          public double nextDouble() throws IOException {
              String x = next();
              StringBuilder sb = new StringBuilder("0");
              double res = 0, f = 1;
              boolean dec = false, neg = false;
              int start = 0;
              if (x.charAt(0) == '-') {
                  neg = true;
                  start++;
              }
              for (int i = start; i < x.length(); i++)
                  if (x.charAt(i) == '.') {
                      res = Long.parseLong(sb.toString());
                      sb = new StringBuilder("0");
                      dec = true;
                  } else {
                      sb.append(x.charAt(i));
                      if (dec)
                          f *= 10;
                  }
              res += Long.parseLong(sb.toString()) / f;
              return res * (neg ? -1 : 1);
          }
  
          public boolean ready() throws IOException {
              return br.ready();
          }
      }
  
  }