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.OutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintWriter;
  import java.util.List;
  import java.util.InputMismatchException;
  import java.io.IOException;
  import java.util.Comparator;
  import java.util.ArrayList;
  import java.io.InputStream;
  
  /**
   * Built using CHelper plug-in
   * Actual solution is at the top
   */
  public class Main {
      public static void main(String[] args) {
          InputStream inputStream = System.in;
          OutputStream outputStream = System.out;
          InputReader in = new InputReader(inputStream);
          PrintWriter out = new PrintWriter(outputStream);
          ESkuchnieOtrezki solver = new ESkuchnieOtrezki();
          solver.solve(1, in, out);
          out.close();
      }
  
      static class ESkuchnieOtrezki {
          public void solve(int testNumber, InputReader in, PrintWriter out) {
              int m = in.nextInt();
              int n = in.nextInt();
              ESkuchnieOtrezki.SegmentTree sg = new ESkuchnieOtrezki.SegmentTree(n);
  
  
              List<ESkuchnieOtrezki.Req> reqs = new ArrayList<>();
              for (int i = 0; i < m; i++) {
                  int from = in.nextInt() - 1;
                  int to = in.nextInt() - 1;
                  if (to != n - 1) {
                      to--;
                  }
                  reqs.add(new ESkuchnieOtrezki.Req(from, to, in.nextInt()));
              }
              reqs.sort(Comparator.comparingInt(a -> a.w));
              int cur = 0;
              int ans = Integer.MAX_VALUE;
  
              for (int i = 0; i < m; i++) {
                  while (cur < m && !sg.check()) {
                      ESkuchnieOtrezki.Req r = reqs.get(cur);
                      sg.update(1, 0, n - 1, r.l, r.r, 1);
                      cur++;
                  }
                  if (!sg.check()) {
                      out.println(ans);
                      return;
                  }
                  ans = Math.min(ans, reqs.get(cur - 1).w - reqs.get(i).w);
                  ESkuchnieOtrezki.Req r = reqs.get(i);
                  sg.update(1, 0, n - 1, r.l, r.r, -1);
              }
              out.println(ans);
          }
  
          public static class Req {
              int l;
              int r;
              int w;
  
              public Req(int l, int r, int w) {
                  this.l = l;
                  this.r = r;
                  this.w = w;
              }
  
          }
  
          public static class SegmentTree {
              public ESkuchnieOtrezki.SegmentTree.Data[] t;
  
              public SegmentTree(int n) {
                  this.t = new ESkuchnieOtrezki.SegmentTree.Data[n * 4 + 1];
                  for (int i = 0; i < n * 4 + 1; i++) {
                      this.t[i] = new ESkuchnieOtrezki.SegmentTree.Data();
                  }
              }
  
              public boolean check() {
                  return this.t[1].min + this.t[1].offset > 0;
              }
  
              public void update(int v, int tl, int tr, int l, int r, int offset) {
                  if (l == tl && tr == r) {
                      t[v].update(offset);
                      return;
                  }
                  int tm = (tl + tr) / 2;
                  if (r <= tm) {
                      update(v * 2, tl, tm, l, r, offset);
                  } else if (l > tm) {
                      update(v * 2 + 1, tm + 1, tr, l, r, offset);
                  } else {
                      update(v * 2, tl, tm, l, Math.min(tm, r), offset);
                      update(v * 2 + 1, tm + 1, tr, Math.max(l, tm + 1), r, offset);
                  }
                  t[v].combine(t[v * 2], t[v * 2 + 1]);
              }
  
              public static class Data {
                  int offset;
                  int min;
  
                  public void update(int val) {
                      this.offset += val;
                  }
  
                  public ESkuchnieOtrezki.SegmentTree.Data combine(ESkuchnieOtrezki.SegmentTree.Data a, ESkuchnieOtrezki.SegmentTree.Data b) {
                      this.min = Math.min(a.min + a.offset, b.min + b.offset);
                      return this;
                  }
  
              }
  
          }
  
      }
  
      static class InputReader {
          private static final int BUFFER_LENGTH = 1 << 10;
          private InputStream stream;
          private byte[] buf = new byte[BUFFER_LENGTH];
          private int curChar;
          private int numChars;
  
          public InputReader(InputStream stream) {
              this.stream = stream;
          }
  
          private int nextC() {
              if (numChars == -1) {
                  throw new InputMismatchException();
              }
              if (curChar >= numChars) {
                  curChar = 0;
                  try {
                      numChars = stream.read(buf);
                  } catch (IOException e) {
                      throw new InputMismatchException();
                  }
                  if (numChars <= 0)
                      return -1;
              }
              return buf[curChar++];
          }
  
          public int nextInt() {
              int c = skipWhileSpace();
              int sgn = 1;
              if (c == '-') {
                  sgn = -1;
                  c = nextC();
              }
              int res = 0;
              do {
                  if (c < '0' || c > '9') {
                      throw new InputMismatchException();
                  }
                  res *= 10;
                  res += c - '0';
                  c = nextC();
              } while (!isSpaceChar(c));
              return res * sgn;
          }
  
          public int skipWhileSpace() {
              int c = nextC();
              while (isSpaceChar(c)) {
                  c = nextC();
              }
              return c;
          }
  
          public boolean isSpaceChar(int c) {
              return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
          }
  
      }
  }
  
  