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 {
  	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  	static StringTokenizer st;
  	static PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
  	static String readLine() throws IOException {
  		return br.readLine();
  	}
  	static String next() throws IOException {
  		while (st == null || !st.hasMoreTokens())
  			st = new StringTokenizer(readLine());
  		return st.nextToken();
  	}
  	static int readInt() throws IOException {
  		return Integer.parseInt(next());
  	}
  	static long readLong() throws IOException {
  		return Long.parseLong(next());
  	}
  	static double readDouble() throws IOException {
  		return Double.parseDouble(next());
  	}
  	static char readChar() throws IOException {
  		return next().charAt(0);
  	}
  	static class Pair implements Comparable<Pair> {
  		int f, s;
  		Pair(int f, int s) {
  			this.f = f; this.s = s;
  		}
  		public int compareTo(Pair other) {
  			if (this.f != other.f) return this.f - other.f;
  			return this.s - other.s;
  		}
  	}
  	static class Segment implements Comparable<Segment> {
  		int L, R, w;
  		Segment(int L, int R, int w) {
  			this.L = L; this.R = R; this.w = w;
  		}
  		public int compareTo(Segment other) {
  			return this.w - other.w;
  		}
  	}
  	final static int inf = (int)1e9;
  	static int t[], lz[];
  	static void push(int v) {
  		if (lz[v] != 0) {
  			t[v<<1] = Math.max(t[v<<1], lz[v]);
  			lz[v<<1] = Math.max(lz[v<<1], lz[v]);
  			t[v<<1|1] = Math.max(t[v<<1|1], lz[v]);
  			lz[v<<1|1] = Math.max(lz[v<<1|1], lz[v]);
  			lz[v] = 0;
  		}
  	}
  	static void update(int qL, int qR, int val, int L, int R, int v) {
  		if (qL <= L && R <= qR) {
  			t[v] = Math.max(t[v], val);
  			lz[v] = Math.max(lz[v], val);
  		} else if (qL <= R && L <= qR) {
  			int M = (L + R)>>1;
  			push(v);
  			update(qL, qR, val, L, M, v<<1);
  			update(qL, qR, val, M + 1, R, v<<1|1);
  			t[v] = Math.min(t[v<<1], t[v<<1|1]);
  		}
  	}
  	static int n, m;
  	static Segment seg[];
  	static void solve() throws IOException {
  		n = readInt();
  		m = readInt();
  		seg = new Segment[n + 1];
  		for (int i = 1; i <= n; ++i) {
  			seg[i] = new Segment(readInt(), readInt(), readInt());
  			if (seg[i].L != 1) ++seg[i].L;
  		}
  		Arrays.sort(seg, 1, n + 1);
  		t = new int[m<<2];
  		lz = new int[m<<2];
  		int ans = inf;
  		for (int i = 1, j = 1; i <= n; ++i) {
  			update(seg[i].L, seg[i].R, seg[i].w, 1, m, 1);
  			if (t[1] >= 1) ans = Math.min(ans, seg[i].w - t[1]);
  		}
  		pr.println(ans);
  	}
  	public static void main(String[] args) throws IOException {
  		solve();
  		//for (int t = readInt(); t > 0; --t) solve();
  		pr.close();
  	}
  }
  