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 {
  
      private static final long MOD = 998244353;
  
      static int[] readArray(int size, InputReader in, boolean subOne) {
          int[] a = new int[size];
          for (int i = 0; i < size; i++) {
              a[i] = in.nextInt() + (subOne ? -1 : 0);
          }
          return a;
      }
  
      static long[] readLongArray(int size, InputReader in) {
          long[] a = new long[size];
          for (int i = 0; i < size; i++) {
              a[i] = in.nextLong();
          }
          return a;
      }
  
      static void sortArray(int[] a) {
          Random random = new Random();
  
          for (int i = 0; i < a.length; i++) {
              int randomPos = random.nextInt(a.length);
              int t = a[i];
              a[i] = a[randomPos];
              a[randomPos] = t;
          }
          Arrays.sort(a);
      }
  
  
      static class ModifyEvalSegTree {
  
          static final long NEUTRAL_ELEMENT = Long.MAX_VALUE;
  
          private int size;
          private final long[] tree;
  
          ModifyEvalSegTree(int n) {
              size = 1;
              while (size < n) {
                  size *= 2;
              }
              tree = new long[2 * size - 1];
              Arrays.fill(tree, NEUTRAL_ELEMENT);
  
          }
  
          private long evaluateOperation(long a, long b) {
              return Math.min(a, b);
          }
  
          private void propagate(int x, int lx, int rx) {
              if (rx - lx == 1 || tree[x] == NEUTRAL_ELEMENT) {
                  return;
              }
  
              tree[2 * x + 2] = evaluateOperation(tree[2 * x + 2], tree[x]);
              tree[2 * x + 1] = evaluateOperation(tree[2 * x + 1], tree[x]);
              tree[x] = NEUTRAL_ELEMENT;
          }
  
          public long result(int minMax) {
              return result(minMax, 0, 0, size);
          }
  
          public long result(int minMax, int x, int lx, int rx) {
              propagate(x, lx, rx);
              if (rx <= minMax) {
                  return Long.MAX_VALUE;
              }
  
              if (rx - lx == 1) {
                  if (tree[x] == NEUTRAL_ELEMENT) {
                      return Long.MAX_VALUE;
                  }
                  return lx - tree[x];
              }
  
              int m = (lx + rx) / 2;
  
              long res1 = result(minMax, 2 * x + 1, lx, m);
              long res2 = result(minMax, 2 * x + 2, m, rx);
  
              return Math.min(res1, res2);
          }
  
  
          public void modify(int l, int r, long v) {
              modify(l, r, v, 0, 0, size);
          }
  
          private void modify(int l, int r, long v, int x, int lx, int rx) {
              propagate(x, lx, rx);
              if (l >= rx || lx >= r) {
                  return;
              }
              if (lx >= l && rx <= r) {
                  tree[x] = evaluateOperation(tree[x], v);
                  return;
              }
              int m = (lx + rx) / 2;
              modify(l, r, v, 2 * x + 1, lx, m);
              modify(l, r, v, 2 * x + 2, m, rx);
          }
      }
  
      public static void main(String[] args) throws FileNotFoundException {
  //        InputReader in = new InputReader(new FileInputStream("input.txt"));
  //        PrintWriter out = new PrintWriter(new BufferedOutputStream(new FileOutputStream("milkvisits.out")));
  //
          InputReader in = new InputReader(System.in);
          PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  
          int n = in.nextInt();
          int m = in.nextInt();
          List<Integer>[] points = new ArrayList[2 * m + 2];
          int maxW = 0;
          for (int i = 0; i < 2 * m + 2; i++) {
              points[i] = new ArrayList<>();
          }
          for (int i = 0; i < n; i++) {
              int l = in.nextInt();
              int r = in.nextInt();
              int w = in.nextInt();
              maxW = Math.max(maxW, w);
              points[2 * l].add(w);
              points[2 * r + 1].add(w);
          }
  
          ModifyEvalSegTree segTree = new ModifyEvalSegTree(maxW + 1);
          TreeMap<Integer, Integer> cnt = new TreeMap<>();
          int minMax = 0;
          for (int i = 2; i <= 2 * m; i++) {
              if (points[i].isEmpty()) {
                  continue;
              }
              if (i % 2 == 1) {
                  points[i].sort(Comparator.comparingInt(o -> o));
                  for (int j = 0; j < points[i].size(); j++) {
                      int endW = points[i].get(j);
                      int cntW = cnt.get(endW);
                      if (cntW == 1) {
                          cnt.remove(endW);
                          Integer floor = cnt.floorKey(endW);
                          if (floor != null) {
                              Integer ceiling = cnt.ceilingKey(endW);
                              if (ceiling == null) {
                                  segTree.modify(floor, maxW + 1, floor);
                              } else {
                                  segTree.modify(floor, ceiling, floor);
                              }
                          }
                      } else {
                          cnt.put(endW, cntW - 1);
                      }
                  }
              } else {
                  points[i].sort(Comparator.comparingInt(o -> -o));
                  for (int j = 0; j < points[i].size(); j++) {
                      int startW = points[i].get(j);
                      Integer ceiling = cnt.ceilingKey(startW);
                      if (!cnt.containsKey(startW)) {
                          if (ceiling == null) {
                              segTree.modify(startW, maxW + 1, startW);
                          } else {
                              segTree.modify(startW, ceiling, startW);
                          }
                          cnt.put(startW, 1);
                      } else {
                          cnt.put(startW, cnt.get(startW) + 1);
                      }
                  }
  
              }
              minMax = Math.max(minMax, cnt.firstKey());
          }
  
          long ans = segTree.result(minMax);
          out.println(ans);
  
          out.close();
      }
  
      private static long solveDummy(List<Integer>[] starts, List<Integer>[] ends, int m, int maxW) {
          long ans = Long.MAX_VALUE;
          for (int w2 = maxW; w2 >= 0; w2--) {
              outer:
              for (int w1 = w2; w1 >= 0; w1--) {
                  int cnt = 0;
                  for (int i = 2; i <= 2 * m + 1; i++) {
                      if (i == 2 * m + 1 && cnt <= 0) {
                          continue outer;
                      }
                      for (int end : ends[i]) {
                          if (end >= w1 && end <= w2) {
                              cnt--;
                          }
                      }
                      if (i % 2 == 1 && i < 2 * m + 1 && cnt <= 0) {
                          continue outer;
                      }
                      for (int start : starts[i]) {
                          if (start >= w1 && start <= w2) {
                              cnt++;
                          }
                      }
                      if (i < 2 * m + 1 && cnt <= 0) {
                          continue outer;
                      }
                  }
                  ans = Math.min(ans, w2 - w1);
              }
          }
          return ans;
      }
  
  
      private static void outputArray(int[] ans, PrintWriter out, boolean addOne) {
          StringBuilder str = new StringBuilder();
          for (int i = 0; i < ans.length; i++) {
              long an = ans[i] + (addOne ? 1 : 0);
              str.append(an).append(' ');
          }
          out.println(str);
      }
  
      private static void outputArray(List<Integer> ans, PrintWriter out, boolean addOne) {
          StringBuilder str = new StringBuilder();
          for (int j = 0; j < ans.size(); j++) {
              long i = ans.get(j);
              long an = i + (addOne ? 1 : 0);
              str.append(an);
              if (j < ans.size() - 1) {
                  str.append(' ');
              }
          }
          out.println(str);
  //        out.flush();
      }
  
      private static class InputReader {
          public BufferedReader reader;
          public StringTokenizer tokenizer;
  
          public InputReader(InputStream stream) {
              reader = new BufferedReader(new InputStreamReader(stream), 32768);
              tokenizer = null;
          }
  
          public String next() {
              while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                  try {
                      tokenizer = new StringTokenizer(reader.readLine());
                  } catch (IOException e) {
                      throw new RuntimeException(e);
                  }
              }
              return tokenizer.nextToken();
          }
  
          public String nextString() {
              try {
                  return reader.readLine();
              } catch (IOException e) {
                  throw new RuntimeException(e);
              }
          }
  
          public int nextInt() {
              return Integer.parseInt(next());
          }
  
          public long nextLong() {
              return Long.parseLong(next());
          }
  
          public double nextDouble() {
              return Double.parseDouble(next());
          }
  
          public char nextChar() {
              return next().charAt(0);
          }
  
          public String nextWord() {
              return next();
          }
  
          private List<Integer>[] readTree(int n) {
              return readGraph(n, n - 1);
          }
  
          private List<Integer>[] readGraph(int n, int m) {
              List<Integer>[] result = new ArrayList[n];
              for (int i = 0; i < n; i++) {
                  result[i] = new ArrayList<>();
              }
              for (int i = 0; i < m; i++) {
                  int u = nextInt() - 1;
                  int v = nextInt() - 1;
                  result[u].add(v);
                  result[v].add(u);
              }
              return result;
          }
  
          private Map<Integer, Long>[] readWeightedGraph(int n, int m) {
              Map<Integer, Long>[] result = new HashMap[n];
              for (int i = 0; i < n; i++) {
                  result[i] = new HashMap<>();
              }
              for (int i = 0; i < m; i++) {
                  int u = nextInt() - 1;
                  int v = nextInt() - 1;
                  long w = nextLong();
                  result[u].put(v, Math.min(w, result[u].getOrDefault(v, Long.MAX_VALUE)));
                  result[v].put(u, Math.min(w, result[v].getOrDefault(u, Long.MAX_VALUE)));
              }
              return result;
          }
  
      }
  }