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: /*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  
  import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  import java.util.StringTokenizer;
  
  /**
   *
   * @author is2ac
   */
  public class D_CF {
  
      public static void main(String[] args) throws IOException {
          FastScanner58 fs = new FastScanner58();
          //Reader fs = new Reader();
          PrintWriter pw = new PrintWriter(System.out);
          //int t = fs.ni();
          int t = 1;
          for (int tc = 0; tc < t; tc++) {
              int n = fs.ni();
              int m = fs.ni();
              List<int[]> list = new ArrayList();
              for (int i = 0; i < n; i++) {
                  int[] a = fs.intArray(3);
                  list.add(a);
              }
              Collections.sort(list,new Comparator<int[]>(){
                  public int compare(int[] a, int[] b) {
                      return a[2]-b[2];
                  }
              });
              int[][] a = new int[n][3];
              for (int i = 0; i < n; i++) {
                  a[i] = list.get(i);
              }
              MinSegTree tree = new MinSegTree(m+1);
              int r = 0;
              int res = (int)(1e9);
              for (int i = 0; i < a.length; i++) {
                  
                  while (r<a.length && tree.query(1,1,m+1,1,m-1)==0) {
                      tree.update(1,1,m+1, a[r][0], a[r][1]-1, 1);
                      r++;
                  }
                  if (tree.query(1,1,m+1,1,m-1)>0) {
                      res = Math.min(res,a[r-1][2]-a[i][2]);
                  }
                  tree.update(1,1,m+1,a[i][0],a[i][1]-1,-1);
              }
              pw.println(res);
          }
          pw.close();
      }
      public static long gcd(long n1, long n2) {
          if (n2 == 0) {
              return n1;
          }
          return gcd(n2, n1 % n2);
      }
  }
  
      class MinSegTree {
          public int[] tree;
          public int[] lazy;
          public int INF;
          public MinSegTree(int size) {
              int MX = size;
              INF = (int)(1e9);
              tree = new int[4*MX];
              lazy = new int[4*MX];
          }
          void pushdown(int x, int l, int r) {
              if (lazy[x] == 0) {
                  return ; 
              }
              tree[x] += lazy[x];
  
              if (l < r) {
                  lazy[2 * x] += lazy[x];
                  lazy[2 * x + 1] += lazy[x];
              }
              lazy[x] = 0;
          }
  
          void update(int x, int l, int r, int tl, int tr, int val) {
              pushdown(x, l, r); 
  
              if (l > tr || r < tl) {
                  return ; 
              }
              if (tl <= l && r <= tr) {
                  lazy[x] = val;
                  pushdown(x, l, r);
                  return ;
              }
              int mid = (l + r) / 2;
  
              update(2 * x, l, mid, tl, tr, val);
              update(2 * x + 1, mid + 1, r, tl, tr, val);
  
              tree[x] = Math.min(tree[2 * x], tree[2 * x + 1]);
          }
          int query(int x, int l, int r, int tl, int tr) {
              pushdown(x, l, r);
  
              if (l > tr || r < tl) {
                  return INF;
              }
              if (tl <= l && r <= tr) {
                  return tree[x];
              }
              int mid = (l+r)/2;
              return Math.min(query(2 * x, l, mid, tl, tr), query(2 * x + 1, mid + 1, r, tl, tr));
          }
      }
  
  class BIT16 {
  
      long[] bit;
  
      public BIT16(int size) {
          bit = new long[size];
      }
  
      public void update(int ind, long delta) {
          while (ind < bit.length) {
              bit[ind] += delta;
              ind = ind + (ind & (-1 * ind));
          }
      }
  
      public long sum(int ind) {
          long s = 0;
          while (ind > 0) {
              s += bit[ind];
              ind = ind - (ind & (-1 * ind));
          }
          return s;
      }
  
      public long query(int l, int r) {
          return sum(r) - sum(l);
      }
  }
  
  class UnionFind18 {
  
      int[] id;
  
      public UnionFind18(int size) {
          id = new int[size];
          for (int i = 0; i < size; i++) {
              id[i] = i;
          }
      }
  
      public int find(int p) {
          int root = p;
          while (root != id[root]) {
              root = id[root];
          }
          while (p != root) {
              int next = id[p];
              id[p] = root;
              p = next;
          }
          return root;
      }
  
      public void union(int p, int q) {
          int a = find(p), b = find(q);
          if (a == b) {
              return;
          }
          id[b] = a;
      }
  }
  
  class Reader {
  
      final private int BUFFER_SIZE = 1 << 16;
      private DataInputStream din;
      private byte[] buffer;
      private int bufferPointer, bytesRead;
  
      public Reader() {
          din = new DataInputStream(System.in);
          buffer = new byte[BUFFER_SIZE];
          bufferPointer = bytesRead = 0;
      }
  
      public Reader(String file_name) throws IOException {
          din = new DataInputStream(new FileInputStream(file_name));
          buffer = new byte[BUFFER_SIZE];
          bufferPointer = bytesRead = 0;
      }
  
      public String readLine() throws IOException {
          byte[] buf = new byte[64];
          int cnt = 0, c;
          while ((c = read()) != -1) {
              if (c == '\n') {
                  break;
              }
              buf[cnt++] = (byte) c;
          }
          return new String(buf, 0, cnt);
      }
  
      public int ni() throws IOException {
          int ret = 0;
          byte c = read();
          while (c <= ' ') {
              c = read();
          }
          boolean neg = (c == '-');
          if (neg) {
              c = read();
          }
          do {
              ret = ret * 10 + c - '0';
          } while ((c = read()) >= '0' && c <= '9');
          if (neg) {
              return -ret;
          }
          return ret;
      }
  
      public long nextLong() throws IOException {
          long ret = 0;
          byte c = read();
          while (c <= ' ') {
              c = read();
          }
          boolean neg = (c == '-');
          if (neg) {
              c = read();
          }
          do {
              ret = ret * 10 + c - '0';
          } while ((c = read()) >= '0' && c <= '9');
          if (neg) {
              return -ret;
          }
          return ret;
      }
  
      public double nextDouble() throws IOException {
          double ret = 0, div = 1;
          byte c = read();
          while (c <= ' ') {
              c = read();
          }
          boolean neg = (c == '-');
          if (neg) {
              c = read();
          }
          do {
              ret = ret * 10 + c - '0';
          } while ((c = read()) >= '0' && c <= '9');
          if (c == '.') {
              while ((c = read()) >= '0' && c <= '9') {
                  ret += (c - '0') / (div *= 10);
              }
          }
          if (neg) {
              return -ret;
          }
          return ret;
      }
  
      private void fillBuffer() throws IOException {
          bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
          if (bytesRead == -1) {
              buffer[0] = -1;
          }
      }
  
      private byte read() throws IOException {
          if (bufferPointer == bytesRead) {
              fillBuffer();
          }
          return buffer[bufferPointer++];
      }
  
      public void close() throws IOException {
          if (din == null) {
              return;
          }
          din.close();
      }
  }
  
  class FastScanner58 {
  
      BufferedReader br;
      StringTokenizer st;
  
      public FastScanner58() {
          br = new BufferedReader(new InputStreamReader(System.in), 32768);
          st = null;
      }
  
      String next() {
          while (st == null || !st.hasMoreElements()) {
              try {
                  st = new StringTokenizer(br.readLine());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return st.nextToken();
      }
  
      int ni() {
          return Integer.parseInt(next());
      }
  
      int[] intArray(int N) {
          int[] ret = new int[N];
          for (int i = 0; i < N; i++) {
              ret[i] = ni();
          }
          return ret;
      }
  
      long nl() {
          return Long.parseLong(next());
      }
  
      long[] longArray(int N) {
          long[] ret = new long[N];
          for (int i = 0; i < N; i++) {
              ret[i] = nl();
          }
          return ret;
      }
  
      double nd() {
          return Double.parseDouble(next());
      }
  
      String nextLine() {
          String str = "";
          try {
              str = br.readLine();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return str;
      }
  }
  