import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
	}
	void _clearU(size_t l, size_t r)
	{
		pragma(inline, true);
		r--;
		auto li = layers - 1;
		for(; li > 0 && (l >> li) == (r >> li); li--) _clearU(l);
		for(; li > 0; li--) _clearU(l >> li), _clearU(r >> li);
	}
	void _refresh(size_t l, size_t r)
	{
		pragma(inline, true);
		r--;
		l >>= 1; r >>= 1;
		for(; l != r; l >>= 1, r >>= 1)
		{
			_nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
			_nF[r] = f(_getNodeF(r<<1), _getNodeF((r<<1)+1));
		}
		for(; l; l >>= 1) _nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
	}
	V rangeF(size_t l, size_t r)
	{
		l += length, r += length;
		_clearU(l, r);
		V value = neutralV;
		_onComps!(c => value = f(value, _getNodeF(c)))(l, r);
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		l += length, r += length;
		_onComps!(c => _addU(c, u))(l, r);
		_refresh(l, r);
	}
	V _getNodeF(size_t i)
	{
		pragma(inline, true);
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		pragma(inline, true);
		return i >= length;
	}
	void _clearU(size_t i)
	{
		pragma(inline, true);
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		pragma(inline, true);
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}
void _onComps(alias act)(size_t l, size_t r)
{
	for(; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) act(l++);
		if (r & 1) act(--r);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
	}
	void _clearU(size_t l, size_t r)
	{
		r--;
		auto li = layers - 1;
		for(; li > 0 && (l >> li) == (r >> li); li--) _clearU(l);
		for(; li > 0; li--) _clearU(l >> li), _clearU(r >> li);
	}
	void _refresh(size_t l, size_t r)
	{
		r--;
		l >>= 1; r >>= 1;
		for(; l != r; l >>= 1, r >>= 1)
		{
			_nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
			_nF[r] = f(_getNodeF(r<<1), _getNodeF((r<<1)+1));
		}
		for(; l; l >>= 1) _nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
	}
	V rangeF(size_t l, size_t r)
	{
		l += length, r += length;
		_clearU(l, r);
		V value;
		bool isSet = false;
		_onComps!((c)
			{ value = isSet? f(value, _getNodeF(c)) : _getNodeF(c); isSet = true; }) (l, r);
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		l += length, r += length;
		_onComps!(c => _addU(c, u))(l, r);
		_refresh(l, r);
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}
void _onComps(alias act)(size_t l, size_t r)
{
	for(; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) act(l++);
		if (r & 1) act(--r);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
	}
	void _clearU(size_t l, size_t r)
	{
		r--;
		auto li = layers - 1;
		for(; li > 0 && (l >> li) == (r >> li); li--) _clearU(l);
		for(; li > 0; li--) _clearU(l >> li), _clearU(r >> li);
	}
	void _refresh(size_t l, size_t r)
	{
		r--;
		l >>= 1; r >>= 1;
		for(; l != r; l >>= 1, r >>= 1)
		{
			_nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
			_nF[r] = f(_getNodeF(r<<1), _getNodeF((r<<1)+1));
		}
		for(; l; l >>= 1) _nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
	}
	V rangeF(size_t l, size_t r)
	{
		l += length, r += length;
		_clearU(l, r);
		V value;
		bool isSet = false;
		_onComps!((c)
				{
					if (isSet)
					{
						value = f(value, _getNodeF(c));
					}
					else
					{
						value = _getNodeF(c);
						isSet = true;
					}
				})(l, r);
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		l += length, r += length;
		_onComps!(c => _addU(c, u))(l, r);
		_refresh(l, r);
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}
void _onComps(alias act)(size_t l, size_t r)
{
	for(; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) act(l++);
		if (r & 1) act(--r);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
	}
	void _clearU(size_t l, size_t r)
	{
		r--;
		auto li = layers - 1;
		for(; li > 0 && (l >> li) == (r >> li); li--) _clearU(l);
		for(; li > 0; li--) _clearU(l >> li), _clearU(r >> li);
	}
	void _refresh(size_t l, size_t r)
	{
		r--;
		l >>= 1; r >>= 1;
		for(; l != r; l >>= 1, r >>= 1)
		{
			_nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
			_nF[r] = f(_getNodeF(r<<1), _getNodeF((r<<1)+1));
		}
		for(; l; l >>= 1) _nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
	}
	V rangeF(size_t l, size_t r)
	{
		l += length, r += length;
		_clearU(l, r);
		V value;
		void delegate(V) add;
		void delegate(V) combine = (V v) { value = f(value, v); };
		void delegate(V) set = (V v) { value = v; add = combine; };
		add = set;
		_onComps!(c => add(_getNodeF(c)))(l, r);
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		l += length, r += length;
		_onComps!(c => _addU(c, u))(l, r);
		_refresh(l, r);
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}
void _onComps(alias act)(size_t l, size_t r)
{
	for(; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) act(l++);
		if (r & 1) act(--r);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
	}
	void _clearU(size_t l, size_t r)
	{
		pragma(inline, true);
		r--;
		auto li = layers - 1;
		for(; li > 0 && (l >> li) == (r >> li); li--) _clearU(l);
		for(; li > 0; li--) _clearU(l >> li), _clearU(r >> li);
	}
	void _refresh(size_t l, size_t r)
	{
		pragma(inline, true);
		r--;
		l >>= 1; r >>= 1;
		for(; l != r; l >>= 1, r >>= 1)
		{
			_nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
			_nF[r] = f(_getNodeF(r<<1), _getNodeF((r<<1)+1));
		}
		for(; l; l >>= 1) _nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
	}
	V rangeF(size_t l, size_t r)
	{
		l += length, r += length;
		_clearU(l, r);
		V value;
		void delegate(V) add;
		void delegate(V) combine = (V v) { value = f(value, v); };
		void delegate(V) set = (V v) { value = v; add = combine; };
		add = set;
		_onComps!(c => add(_getNodeF(c)))(l, r);
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		l += length, r += length;
		_onComps!(c => _addU(c, u))(l, r);
		_refresh(l, r);
	}
	V _getNodeF(size_t i)
	{
		pragma(inline, true);
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		pragma(inline, true);
		return i >= length;
	}
	void _clearU(size_t i)
	{
		pragma(inline, true);
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		pragma(inline, true);
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}
void _onComps(alias act)(size_t l, size_t r)
{
	for(; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) act(l++);
		if (r & 1) act(--r);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
	}
	void _clearU(size_t l, size_t r)
	{
		pragma(inline, true);
		r--;
		auto li = layers - 1;
		for(; li > 0 && (l >> li) == (r >> li); li--) _clearU(l);
		for(; li > 0; li--) _clearU(l >> li), _clearU(r >> li);
	}
	void _refresh(size_t l, size_t r)
	{
		pragma(inline, true);
		r--;
		l >>= 1; r >>= 1;
		for(; l != r; l >>= 1, r >>= 1)
		{
			_nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
			_nF[r] = f(_getNodeF(r<<1), _getNodeF((r<<1)+1));
		}
		for(; l; l >>= 1) _nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
	}
	V rangeF(size_t l, size_t r)
	{
		l += length, r += length;
		_clearU(l, r);
		V value;
		void delegate(V) add;
		void delegate(V) combine = (V v) { value = f(value, v); };
		void delegate(V) set = (V v) { value = v; add = combine; };
		add = set;
		_onComps!(c => add(_getNodeF(c)))(l, r);
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		l += length, r += length;
		_onComps!(c => _addU(c, u))(l, r);
		_refresh(l, r);
	}
	V _getNodeF(size_t i)
	{
		pragma(inline, true);
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		pragma(inline, true);
		return i >= length;
	}
	void _clearU(size_t i)
	{
		pragma(inline, true);
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		pragma(inline, true);
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}
void _onComps(alias act)(size_t l, size_t r)
{
	for(; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) act(l++);
		if (r & 1) act(--r);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
	}
	void _clearU(size_t l, size_t r)
	{
		r--;
		auto li = layers - 1;
		for(; li > 0 && (l >> li) == (r >> li); li--) _clearU(l);
		for(; li > 0; li--) _clearU(l >> li), _clearU(r >> li);
	}
	void _refresh(size_t l, size_t r)
	{
		r--;
		l >>= 1; r >>= 1;
		for(; l != r; l >>= 1, r >>= 1)
		{
			_nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
			_nF[r] = f(_getNodeF(r<<1), _getNodeF((r<<1)+1));
		}
		for(; l; l >>= 1) _nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
	}
	V rangeF(size_t l, size_t r)
	{
		l += length, r += length;
		_clearU(l, r);
		V value = neutralV;
		_onComps!(c => value = f(value, _getNodeF(c)))(l, r);
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		l += length, r += length;
		_onComps!(c => _addU(c, u))(l, r);
		_refresh(l, r);
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}
void _onComps(alias act)(size_t l, size_t r)
{
	for(; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) act(l++);
		if (r & 1) act(--r);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
		decomp.components = new size_t[](2 * layers);
		decomp.affected = new size_t[](2 * layers);
	}
	struct Decomp
	{
		size_t[] components;
		size_t noComponents;
		size_t[] affected;
		size_t noAffected;
	}
	Decomp decomp;
	void _getDecomp(size_t l, size_t r)
	{
		decomp.noAffected = 0, decomp.noComponents = 0;
		l += length, r += length;
		size_t ol = l, or = r - 1;
		for(; l < r; l >>= 1, r >>= 1)
		{
			if (l & 1)
				decomp.components[decomp.noComponents++] = l++;
			if (r & 1)
				decomp.components[decomp.noComponents++] = --r;
		}
		ol >>= 1; or >>= 1;
		while (ol != or)
		{
			decomp.affected[decomp.noAffected++] = ol;
			decomp.affected[decomp.noAffected++] = or;
			ol >>= 1, or >>= 1;
		}
		while (ol)
		{
			decomp.affected[decomp.noAffected++] = ol;
			ol >>= 1;
		}
	}
	V rangeF(size_t l, size_t r)
	{
		_getDecomp(l, r);
		decomp.affected[0 .. decomp.noAffected].retro.each!(i => _clearU(i));
		return decomp.components[0 .. decomp.noComponents].map!(i => _getNodeF(i)).fold!f;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		_getDecomp(l, r);
		foreach(i; decomp.components[0 .. decomp.noComponents]) _addU(i, u);
		foreach(i; decomp.affected[0 .. decomp.noAffected]) _nF[i] = f(_getNodeF(i << 1), _getNodeF((i << 1)+1));
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
		decomp.components = new size_t[](2 * layers);
		decomp.affected = new size_t[](2 * layers);
	}
	struct Decomp
	{
		size_t[] components;
		size_t noComponents;
		size_t[] affected;
		size_t noAffected;
	}
	Decomp decomp;
	void _getDecomp(size_t l, size_t r)
	{
		decomp.noAffected = 0, decomp.noComponents = 0;
		l += length, r += length;
		size_t ol = l, or = r - 1;
		for(; l < r; l >>= 1, r >>= 1)
		{
			if (l & 1)
				decomp.components[decomp.noComponents++] = l++;
			if (r & 1)
				decomp.components[decomp.noComponents++] = --r;
		}
		ol >>= 1; or >>= 1;
		while (ol != or)
		{
			decomp.affected[decomp.noAffected++] = ol;
			decomp.affected[decomp.noAffected++] = or;
			ol >>= 1, or >>= 1;
		}
		while (ol)
		{
			decomp.affected[decomp.noAffected++] = ol;
			ol >>= 1;
		}
	}
	V rangeF(size_t l, size_t r)
	{
		_getDecomp(l, r);
		foreach_reverse(i; decomp.affected[0 .. decomp.noAffected]) _clearU(i);
		V value = neutralV;
		foreach(i; decomp.components[0 .. decomp.noComponents]) value = f(value, _getNodeF(i));
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		_getDecomp(l, r);
		foreach(i; decomp.components[0 .. decomp.noComponents]) _addU(i, u);
		foreach(i; decomp.affected[0 .. decomp.noAffected]) _nF[i] = f(_getNodeF(i << 1), _getNodeF((i << 1)+1));
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = -1;
	int j = 0;
	while (i + 1 < n && st.rangeF(0, m - 1) == 0) { i++; st.rangeU(segs[i][1], segs[i][2], 1); }
	if (st.rangeF(0, m - 1) == 0) assert(0);
	while (i < n)
	{
		while (st.rangeF(0, m - 1) > 0) remove(j++);
		add(--j);
		ans = min(ans, segs[i][0] - segs[j][0]);
		if (++i < n) add(i);
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
		decomp.components = new size_t[](2 * layers);
		decomp.affected = new size_t[](2 * layers);
	}
	struct Decomp
	{
		size_t[] components;
		size_t noComponents;
		size_t[] affected;
		size_t noAffected;
	}
	Decomp decomp;
	void _getDecomp(size_t l, size_t r)
	{
		decomp.noAffected = 0, decomp.noComponents = 0;
		l += length, r += length;
		size_t ol = l, or = r - 1;
		for(; l < r; l >>= 1, r >>= 1)
		{
			if (l & 1)
				decomp.components[decomp.noComponents++] = l++;
			if (r & 1)
				decomp.components[decomp.noComponents++] = --r;
		}
		ol >>= 1; or >>= 1;
		while (ol != or)
		{
			decomp.affected[decomp.noAffected++] = ol;
			decomp.affected[decomp.noAffected++] = or;
			ol >>= 1, or >>= 1;
		}
		while (ol)
		{
			decomp.affected[decomp.noAffected++] = ol;
			ol >>= 1;
		}
	}
	V rangeF(size_t l, size_t r)
	{
		_getDecomp(l, r);
		foreach_reverse(i; decomp.affected[0 .. decomp.noAffected]) _clearU(i);
		V value = neutralV;
		foreach(i; decomp.components[0 .. decomp.noComponents]) value = f(value, _getNodeF(i));
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		_getDecomp(l, r);
		foreach(i; decomp.components[0 .. decomp.noComponents]) _addU(i, u);
		foreach(i; decomp.affected[0 .. decomp.noAffected]) _nF[i] = f(_getNodeF(i << 1), _getNodeF((i << 1)+1));
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = -1;
	int j = 0;
	while (i + 1 < n && st.rangeF(0, m - 1) == 0) { i++; st.rangeU(segs[i][1], segs[i][2], 1); }
	if (st.rangeF(0, m - 1) == 0) assert(0);
	while (i < n)
	{
		while (st.rangeF(0, m - 1) > 0) remove(j++);
		add(--j);
		ans = min(ans, segs[i][0] - segs[j][0]);
		if (++i < n) add(i);
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
		decomp.components = new size_t[](2 * layers);
		decomp.affected = new size_t[](2 * layers);
	}
	struct Decomp
	{
		size_t[] components;
		size_t noComponents;
		size_t[] affected;
		size_t noAffected;
	}
	Decomp decomp;
	void _getDecomp(size_t l, size_t r)
	{
		decomp.noAffected = 0, decomp.noComponents = 0;
		l += length, r += length;
		size_t ol = l, or = r - 1;
		for(; l < r; l >>= 1, r >>= 1)
		{
			if (l & 1)
			{
				decomp.components[decomp.noComponents++] = l;
				l++;
			}
			if (r & 1)
			{
				decomp.components[decomp.noComponents++] = r - 1;
				r--;
			}
		}
		ol >>= 1; or >>= 1;
		while (ol > 0 && or > 0)
		{
			if (ol != or)
			{
				decomp.affected[decomp.noAffected++] = ol;
				decomp.affected[decomp.noAffected++] = or;
			}
			else decomp.affected[decomp.noAffected++] = ol;
			ol >>= 1; or >>= 1;
		}
	}
	V rangeF(size_t l, size_t r)
	{
		_getDecomp(l, r);
		foreach_reverse(i; decomp.affected[0 .. decomp.noAffected]) _clearU(i);
		V value = neutralV;
		foreach(i; decomp.components[0 .. decomp.noComponents]) value = f(value, _getNodeF(i));
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		_getDecomp(l, r);
		foreach(i; decomp.components[0 .. decomp.noComponents]) _addU(i, u);
		foreach(i; decomp.affected[0 .. decomp.noAffected]) _nF[i] = f(_getNodeF(i << 1), _getNodeF((i << 1)+1));
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = -1;
	int j = 0;
	while (i + 1 < n && st.rangeF(0, m - 1) == 0) { i++; st.rangeU(segs[i][1], segs[i][2], 1); }
	if (st.rangeF(0, m - 1) == 0) assert(0);
	while (i < n)
	{
		while (st.rangeF(0, m - 1) > 0) remove(j++);
		add(--j);
		ans = min(ans, segs[i][0] - segs[j][0]);
		if (++i < n) add(i);
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
		decomp.components = new size_t[](2 * layers);
		decomp.affected = new size_t[](2 * layers);
	}
	struct Decomp
	{
		size_t[] components;
		size_t noComponents;
		size_t[] affected;
		size_t noAffected;
	}
	Decomp decomp;
	void _getDecomp(size_t l, size_t r)
	{
		decomp.noAffected = 0, decomp.noComponents = 0;
		l += length, r += length;
		size_t ol = l, or = r - 1;
		for(; l < r; l >>= 1, r >>= 1)
		{
			if (l & 1)
			{
				decomp.components[decomp.noComponents++] = l;
				l++;
			}
			if (r & 1)
			{
				decomp.components[decomp.noComponents++] = r - 1;
				r--;
			}
		}
		ol >>= 1; or >>= 1;
		while (ol > 0 && or > 0)
		{
			if (ol != or)
			{
				decomp.affected[decomp.noAffected++] = ol;
				decomp.affected[decomp.noAffected++] = or;
			}
			else decomp.affected[decomp.noAffected++] = ol;
			ol >>= 1; or >>= 1;
		}
	}
	V rangeF(size_t l, size_t r)
	{
		_getDecomp(l, r);
		foreach_reverse(i; decomp.affected[0 .. decomp.noAffected]) _clearU(i);
		V value = neutralV;
		foreach(i; decomp.components[0 .. decomp.noComponents]) value = f(value, _getNodeF(i));
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		_getDecomp(l, r);
		foreach_reverse(i; decomp.affected[0 .. decomp.noAffected]) _clearU(i);
		foreach(i; decomp.components[0 .. decomp.noComponents]) _addU(i, u);
		foreach(i; decomp.affected[0 .. decomp.noAffected]) _nF[i] = f(_getNodeF(i << 1), _getNodeF((i << 1)+1));
	}
	V _getNodeF(size_t i)
	{
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		return i >= length;
	}
	void _clearU(size_t i)
	{
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = SegmentTreeLazy!(int, int,
			(a, b) => min(a, b),
			(mn, u) => mn + u,
			(u1, u2) => u1 + u2,
			"min",
			"+")(m - 1, 0);
	alias add = i => st[segs[i][1] .. segs[i][2]] += 1;
	alias remove = i => st[segs[i][1] .. segs[i][2]] += -1;
	int ans = int.max;
	int i = -1;
	int j = 0;
	debug writeln(segs);
	while (st[0 .. m - 1].min == 0)
	{
		add(i + 1);
		i++;
	}
	while (i < n)
	{
		while (st[0 .. m - 1].min > 0) remove(j++);
		add(--j);
		ans = min(ans, segs[i][0] - segs[j][0]);
		if (++i < n) add(i);
	}
	ans.writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

import std.algorithm;
import std.random;

struct SegmentTreeLazy(V, U, alias f, alias fou, alias uou, string fName = null, string uName = null)
{
	alias This = typeof(this);
	size_t length;
	size_t realLength = 0;
	V[] _nodeFValue;
	U[] _nodeUpdate;
	bool[] _nodeHasUpdate;
	size_t[] _nodeRangeEnd;
	size_t[] _nodeRangeStart;
	V[] _cookedFCache;
	string toString()
	{
		import std.conv;
		string str;
		foreach(i; 0 .. realLength)
			if (_nodeHasUpdate[i])
			{
				str ~= text(_nodeFValue[i], "(", _nodeUpdate[i], "), ");
			}
			else
			{
				str ~= text(_nodeFValue[i], ", ");
			}
		return str;
	}
	this(size_t length, V initialValue)
	{
		this.length = length;
		size_t noNodes = 4 * length;
		_nodeFValue = new V[](noNodes);
		_nodeUpdate = new U[](noNodes);
		_nodeHasUpdate = new bool[](noNodes);
		_nodeRangeEnd = new size_t[](noNodes);
		_nodeRangeStart = new size_t[](noNodes);
		_cookedFCache = new V[](noNodes);
		void fill(size_t i, size_t rangeStart, size_t rangeEnd)
		{
			realLength = max(realLength, i + 1);
			_nodeRangeStart[i] = rangeStart;
			_nodeRangeEnd[i] = rangeEnd;
			if (_isLeaf(i)) { _nodeFValue[i] = initialValue; return; }
			size_t l = i<<1;
			size_t r = l+1;
			size_t m = (rangeStart + rangeEnd) / 2;
			fill(l, rangeStart, m), fill(r, m + 1, rangeEnd);
			_nodeFValue[i] = f(_nodeFValue[l], _nodeFValue[r]);
			_cookedFCache[i] = _nodeFValue[i];
		}
		fill(1, 0, length - 1);
	}

	size_t _qStart, _qEnd;
	bool _set = false;
	V _result;
	V rangeF(size_t start, size_t end)
	{
		pragma(inline, true);
		_set = false;
		_qStart = start;
		_qEnd = end;
		visitF(1);
		return _result;
	}
	void visitF(size_t i) in(_hasFragments(i))
	{
		if (_isFragment(i))
		{
			if (!_set) { _result = _cookedFCache[i]; _set = true; }
			else { _result = f(_result, _cookedFCache[i]); }
			return;
		}
		_clearUpdate(i);
		size_t l = i<<1, r = (i<<1)+1;
		if (_qEnd <= _nodeRangeEnd[l]) {visitF(l);return;}
		if (_qStart >= _nodeRangeStart[r]) {visitF(r);return;}
		visitF(l), visitF(r);
	}

	U _qU;
	void rangeU(size_t start, size_t end, U u)
	{
		pragma(inline, true);
		_qStart = start;
		_qEnd = end;
		_qU = u;
		visitU(1);
	}
	void visitU(size_t i) in(_hasFragments(i))
	{
		if (_isFragment(i))
		{
			_addUpdate(i, _qU);
			return;
		}
		_clearUpdate(i);
		size_t l = i<<1, r = (i<<1)+1;
		if (_qEnd <= _nodeRangeEnd[l]) {visitU(l);goto end;}
		if (_qStart >= _nodeRangeStart[r]) {visitU(r);goto end;}
		visitU(l), visitU(r);
end:
		assert(!_nodeHasUpdate[i]);
		_nodeFValue[i] = f(_cookedFCache[l], _cookedFCache[r]);
		_cookedFCache[i] = _nodeFValue[i];
	}
	V _cookedF(size_t i) in(_nodeHasUpdate[i])
	{
		pragma(inline, true);
		import std.traits;
		return fou(_nodeFValue[i],
			    _nodeUpdate[i]);
	}
	void _clearUpdate(size_t i) out(;!_nodeHasUpdate[i])
	{
		pragma(inline, true);
		if (!_nodeHasUpdate[i]) return;
		if (_isLeaf(i)) { _nodeFValue[i] = _cookedFCache[i]; _nodeHasUpdate[i] = false; return;}
		size_t l = i<<1, r = (i<<1)+1;
		_addUpdate(l, _nodeUpdate[i]), _addUpdate(r, _nodeUpdate[i]);
		_nodeFValue[i] = f(_cookedFCache[l], _cookedFCache[r]);
		_cookedFCache[i] = _nodeFValue[i];
		_nodeHasUpdate[i] = false;
	}
	void _addUpdate(size_t i, U u)
	{
		pragma(inline, true);
		if (!_nodeHasUpdate[i]) { _nodeHasUpdate[i] = true; _nodeUpdate[i] = u; _cookedFCache[i] = _cookedF(i); return; }
		_nodeUpdate[i] = uou(_nodeUpdate[i], u);
		_cookedFCache[i] = _cookedF(i);
	}
	bool _isLeaf(size_t i)
	{
		pragma(inline, true);
		return _nodeRangeStart[i] == _nodeRangeEnd[i];
	}
	bool _isFragment(size_t i)
	{
		pragma(inline, true);
		return _qStart <= _nodeRangeStart[i] && _nodeRangeEnd[i] <= _qEnd;
	}
	bool _hasFragments(size_t i)
	{
		pragma(inline, true);
		return _nodeRangeStart[i] <= _qEnd && _qStart <= _nodeRangeEnd[i];
	}
	// prettifiers
	struct Handle
	{
		import std.ascii;
		size_t _start, _end;
		This* _parent;
		auto f()
		{
			return _parent.rangeF(_start, _end);
		}
		auto u(U v)
		{
			_parent.rangeU(_start, _end, v);
		}
		static if (fName !is null)
		{
			mixin(q{alias }, fName, q{ = f;});
		}
		static if (uName !is null)
		{
			static if (uName[0].isAlphaNum)
			{
				mixin(q{alias }, uName, q{ = u;});
			}
			else static if (uName[0] != ' ')
			{
				ref Handle opOpAssign(string operator)(U v)
				if (operator == uName)
				{
					u(v);
					return this;
				}
			}
			else
			{
				ref Handle opAssign(U v)
				{
					u(v);
					return this;
				}
			}
		}
	}
	size_t[2] opSlice(size_t i)(size_t s, size_t e) if (i == 0)
	{
		return [s, e-1];
	}
	auto opIndex(size_t[2] slice)
	{
		return Handle(slice[0], slice[1], &this);
	}
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = SegmentTreeLazy!(int, int,
			(a, b) => min(a, b),
			(mn, u) => mn + u,
			(u1, u2) => u1 + u2,
			"min",
			"+")(m, 0);
	int ans = int.max;
	int i = -1;
	int j = 0;
	debug writeln(segs);
	while (st[0 .. m - 1] == 0)
	{
		debug writeln("adding segment ", segs[i+1]);
		st[segs[i+1][1] .. segs[i+1][2]] = +1;
		debug foreach(k; 0 .. m) st[k .. k + 1].write(" ");
		debug writeln;
		i++;
	}
	while (i < n)
	{
		while (st[0 .. m - 1] > 0)
		{
			st[segs[j][1] .. segs[j][2]] = -1;
			j++;
		}
		st[segs[j-1][1] .. segs[j-1][2]] = +1;
		j--;
		assert(st[0 .. m - 1] > 0);
		ans = min(ans, segs[i][0] - segs[j][0]);
		if (i + 1 < n)
			st[segs[i + 1][1] .. segs[i + 1][2]] = +1;
		i++;
	}
	ans.writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

import std.algorithm;
import std.random;

struct SegmentTreeLazy(V, U, alias f, alias fou, alias uou, string fName = null, string uName = null)
{
	alias This = typeof(this);
	size_t length;
	size_t realLength = 0;
	V[] _nodeFValue;
	U[] _nodeUpdate;
	bool[] _nodeHasUpdate;
	size_t[] _nodeRangeEnd;
	size_t[] _nodeRangeStart;
	V[] _cookedFCache;
	string toString()
	{
		import std.conv;
		string str;
		foreach(i; 0 .. realLength)
			if (_nodeHasUpdate[i])
			{
				str ~= text(_nodeFValue[i], "(", _nodeUpdate[i], "), ");
			}
			else
			{
				str ~= text(_nodeFValue[i], ", ");
			}
		return str;
	}
	this(size_t length, V initialValue)
	{
		this.length = length;
		size_t noNodes = 4 * length;
		_nodeFValue = new V[](noNodes);
		_nodeUpdate = new U[](noNodes);
		_nodeHasUpdate = new bool[](noNodes);
		_nodeRangeEnd = new size_t[](noNodes);
		_nodeRangeStart = new size_t[](noNodes);
		_cookedFCache = new V[](noNodes);
		void fill(size_t i, size_t rangeStart, size_t rangeEnd)
		{
			realLength = max(realLength, i + 1);
			_nodeRangeStart[i] = rangeStart;
			_nodeRangeEnd[i] = rangeEnd;
			if (_isLeaf(i)) { _nodeFValue[i] = initialValue; return; }
			size_t l = i<<1;
			size_t r = l+1;
			size_t m = (rangeStart + rangeEnd) / 2;
			fill(l, rangeStart, m), fill(r, m + 1, rangeEnd);
			_nodeFValue[i] = f(_nodeFValue[l], _nodeFValue[r]);
			_cookedFCache[i] = _nodeFValue[i];
		}
		fill(1, 0, length - 1);
	}

	size_t _qStart, _qEnd;
	bool _set = false;
	V _result;
	V rangeF(size_t start, size_t end)
	{
		pragma(inline, true);
		_set = false;
		_qStart = start;
		_qEnd = end;
		visitF(1);
		return _result;
	}
	void visitF(size_t i) in(_hasFragments(i))
	{
		if (_isFragment(i))
		{
			if (!_set) { _result = _cookedFCache[i]; _set = true; }
			else { _result = f(_result, _cookedFCache[i]); }
			return;
		}
		_clearUpdate(i);
		size_t l = i<<1, r = (i<<1)+1;
		if (_qEnd <= _nodeRangeEnd[l]) {visitF(l);return;}
		if (_qStart >= _nodeRangeStart[r]) {visitF(r);return;}
		visitF(l), visitF(r);
	}

	U _qU;
	void rangeU(size_t start, size_t end, U u)
	{
		pragma(inline, true);
		_qStart = start;
		_qEnd = end;
		_qU = u;
		visitU(1);
	}
	void visitU(size_t i) in(_hasFragments(i))
	{
		if (_isFragment(i))
		{
			_addUpdate(i, _qU);
			return;
		}
		_clearUpdate(i);
		size_t l = i<<1, r = (i<<1)+1;
		if (_qEnd <= _nodeRangeEnd[l]) {visitU(l);goto end;}
		if (_qStart >= _nodeRangeStart[r]) {visitU(r);goto end;}
		visitU(l), visitU(r);
end:
		assert(!_nodeHasUpdate[i]);
		_nodeFValue[i] = f(_cookedFCache[l], _cookedFCache[r]);
		_cookedFCache[i] = _nodeFValue[i];
	}
	V _cookedF(size_t i) in(_nodeHasUpdate[i])
	{
		pragma(inline, true);
		import std.traits;
		return fou(_nodeFValue[i],
			    _nodeUpdate[i]);
	}
	void _clearUpdate(size_t i) out(;!_nodeHasUpdate[i])
	{
		pragma(inline, true);
		if (!_nodeHasUpdate[i]) return;
		if (_isLeaf(i)) { _nodeFValue[i] = _cookedFCache[i]; _nodeHasUpdate[i] = false; return;}
		size_t l = i<<1, r = (i<<1)+1;
		_addUpdate(l, _nodeUpdate[i]), _addUpdate(r, _nodeUpdate[i]);
		_nodeFValue[i] = f(_cookedFCache[l], _cookedFCache[r]);
		_cookedFCache[i] = _nodeFValue[i];
		_nodeHasUpdate[i] = false;
	}
	void _addUpdate(size_t i, U u)
	{
		pragma(inline, true);
		if (!_nodeHasUpdate[i]) { _nodeHasUpdate[i] = true; _nodeUpdate[i] = u; _cookedFCache[i] = _cookedF(i); return; }
		_nodeUpdate[i] = uou(_nodeUpdate[i], u);
		_cookedFCache[i] = _cookedF(i);
	}
	bool _isLeaf(size_t i)
	{
		pragma(inline, true);
		return _nodeRangeStart[i] == _nodeRangeEnd[i];
	}
	bool _isFragment(size_t i)
	{
		pragma(inline, true);
		return _qStart <= _nodeRangeStart[i] && _nodeRangeEnd[i] <= _qEnd;
	}
	bool _hasFragments(size_t i)
	{
		pragma(inline, true);
		return _nodeRangeStart[i] <= _qEnd && _qStart <= _nodeRangeEnd[i];
	}
	size_t[2] opSlice(size_t i)(size_t s, size_t e) if (i == 0)
	{
		return [s, e-1];
	}
	auto opIndex(size_t[2] slice)
	{
		return rangeF(slice[0], slice[1]);
	}
	void opIndexAssign(U u, size_t[2] slice)
	{
		return rangeU(slice[0], slice[1], u);
	}
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = SegmentTreeLazy!(int, int,
			(a, b) => min(a, b),
			(mn, u) => mn + u,
			(u1, u2) => u1 + u2,
			"min",
			"+")(m, 0);
	int ans = int.max;
	int i = -1;
	int j = 0;
	debug writeln(segs);
	while (st[0 .. m - 1].min == 0)
	{
		debug writeln("adding segment ", segs[i+1]);
		st[segs[i+1][1] .. segs[i+1][2]] += +1;
		debug foreach(k; 0 .. m) st[k .. k + 1].min.write(" ");
		debug writeln;
		i++;
	}
	while (i < n)
	{
		while (st[0 .. m - 1].min > 0)
		{
			st[segs[j][1] .. segs[j][2]] += -1;
			j++;
		}
		st[segs[j-1][1] .. segs[j-1][2]] += +1;
		j--;
		assert(st[0 .. m - 1].min > 0);
		ans = min(ans, segs[i][0] - segs[j][0]);
		if (i + 1 < n)
			st[segs[i + 1][1] .. segs[i + 1][2]] += +1;
		i++;
	}
	ans.writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

import std.algorithm;
import std.random;

struct SegmentTreeLazy(V, U, alias f, alias fou, alias uou, string fName = null, string uName = null)
{
	alias This = typeof(this);
	size_t length;
	size_t realLength = 0;
	V[] _nodeFValue;
	U[] _nodeUpdate;
	bool[] _nodeHasUpdate;
	size_t[] _nodeRangeEnd;
	size_t[] _nodeRangeStart;
	V[] _cookedFCache;
	string toString()
	{
		import std.conv;
		string str;
		foreach(i; 0 .. realLength)
			if (_nodeHasUpdate[i])
			{
				str ~= text(_nodeFValue[i], "(", _nodeUpdate[i], "), ");
			}
			else
			{
				str ~= text(_nodeFValue[i], ", ");
			}
		return str;
	}
	this(size_t length, V initialValue)
	{
		this.length = length;
		size_t noNodes = 4 * length;
		_nodeFValue = new V[](noNodes);
		_nodeUpdate = new U[](noNodes);
		_nodeHasUpdate = new bool[](noNodes);
		_nodeRangeEnd = new size_t[](noNodes);
		_nodeRangeStart = new size_t[](noNodes);
		_cookedFCache = new V[](noNodes);
		void fill(size_t i, size_t rangeStart, size_t rangeEnd)
		{
			realLength = max(realLength, i + 1);
			_nodeRangeStart[i] = rangeStart;
			_nodeRangeEnd[i] = rangeEnd;
			if (_isLeaf(i)) { _nodeFValue[i] = initialValue; return; }
			size_t l = i<<1;
			size_t r = l+1;
			size_t m = (rangeStart + rangeEnd) / 2;
			fill(l, rangeStart, m), fill(r, m + 1, rangeEnd);
			_nodeFValue[i] = f(_nodeFValue[l], _nodeFValue[r]);
			_cookedFCache[i] = _nodeFValue[i];
		}
		fill(1, 0, length - 1);
	}

	size_t _qStart, _qEnd;
	bool _set = false;
	V _result;
	V rangeF(size_t start, size_t end)
	{
		pragma(inline, true);
		_set = false;
		_qStart = start;
		_qEnd = end;
		visitF(1);
		return _result;
	}
	void visitF(size_t i) in(_hasFragments(i))
	{
		if (_isFragment(i))
		{
			if (!_set) { _result = _cookedFCache[i]; _set = true; }
			else { _result = f(_result, _cookedFCache[i]); }
			return;
		}
		_clearUpdate(i);
		size_t l = i<<1, r = (i<<1)+1;
		if (_qEnd <= _nodeRangeEnd[l]) {visitF(l);return;}
		if (_qStart >= _nodeRangeStart[r]) {visitF(r);return;}
		visitF(l), visitF(r);
	}

	U _qU;
	void rangeU(size_t start, size_t end, U u)
	{
		pragma(inline, true);
		_qStart = start;
		_qEnd = end;
		_qU = u;
		visitU(1);
	}
	void visitU(size_t i) in(_hasFragments(i))
	{
		if (_isFragment(i))
		{
			_addUpdate(i, _qU);
			return;
		}
		_clearUpdate(i);
		size_t l = i<<1, r = (i<<1)+1;
		if (_qEnd <= _nodeRangeEnd[l]) {visitU(l);goto end;}
		if (_qStart >= _nodeRangeStart[r]) {visitU(r);goto end;}
		visitU(l), visitU(r);
end:
		assert(!_nodeHasUpdate[i]);
		_nodeFValue[i] = f(_cookedFCache[l], _cookedFCache[r]);
		_cookedFCache[i] = _nodeFValue[i];
	}
	V _cookedF(size_t i) in(_nodeHasUpdate[i])
	{
		pragma(inline, true);
		import std.traits;
		return fou(_nodeFValue[i],
			    _nodeUpdate[i]);
	}
	void _clearUpdate(size_t i) out(;!_nodeHasUpdate[i])
	{
		pragma(inline, true);
		if (!_nodeHasUpdate[i]) return;
		if (_isLeaf(i)) { _nodeFValue[i] = _cookedFCache[i]; _nodeHasUpdate[i] = false; return;}
		size_t l = i<<1, r = (i<<1)+1;
		_addUpdate(l, _nodeUpdate[i]), _addUpdate(r, _nodeUpdate[i]);
		_nodeFValue[i] = f(_cookedFCache[l], _cookedFCache[r]);
		_cookedFCache[i] = _nodeFValue[i];
		_nodeHasUpdate[i] = false;
	}
	void _addUpdate(size_t i, U u)
	{
		pragma(inline, true);
		if (!_nodeHasUpdate[i]) { _nodeHasUpdate[i] = true; _nodeUpdate[i] = u; _cookedFCache[i] = _cookedF(i); return; }
		_nodeUpdate[i] = uou(_nodeUpdate[i], u);
		_cookedFCache[i] = _cookedF(i);
	}
	bool _isLeaf(size_t i)
	{
		pragma(inline, true);
		return _nodeRangeStart[i] == _nodeRangeEnd[i];
	}
	bool _isFragment(size_t i)
	{
		pragma(inline, true);
		return _qStart <= _nodeRangeStart[i] && _nodeRangeEnd[i] <= _qEnd;
	}
	bool _hasFragments(size_t i)
	{
		pragma(inline, true);
		return _nodeRangeStart[i] <= _qEnd && _qStart <= _nodeRangeEnd[i];
	}
	// prettifiers
	struct Handle
	{
		import std.ascii;
		size_t _start, _end;
		This* _parent;
		auto f()
		{
			return _parent.rangeF(_start, _end);
		}
		auto u(U v)
		{
			_parent.rangeU(_start, _end, v);
		}
		static if (fName !is null)
		{
			mixin(q{alias }, fName, q{ = f;});
		}
		static if (uName !is null)
		{
			static if (uName[0].isAlphaNum)
			{
				mixin(q{alias }, uName, q{ = u;});
			}
			else static if (uName[0] != ' ')
			{
				ref Handle opOpAssign(string operator)(U v)
				if (operator == uName)
				{
					u(v);
					return this;
				}
			}
			else
			{
				ref Handle opAssign(U v)
				{
					u(v);
					return this;
				}
			}
		}
	}
	size_t[2] opSlice(size_t i)(size_t s, size_t e) if (i == 0)
	{
		return [s, e-1];
	}
	auto opIndex(size_t[2] slice)
	{
		return Handle(slice[0], slice[1], &this);
	}
}

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

long nextPow2(long n)
{
	int i = 0;
	while (n)
	{
		n >>= 1;
		i++;
	}
	if ((1L << i) >= n) return 1L << i;
	return (1L << (i + 1));
}

version(none) void main()
{
	auto st = STL!(int, 0, int, (a, b) => a + b, (s, i, len) => s + i * len, (nu, ou) => nu + ou)(100000, 0);
	auto brute = new int[](st.length);
	foreach(i; 0 .. 100000)
	{
		int l = uniform!"[]"(0, cast(int)st.length - 1);
		int r = uniform!"[]"(l + 1, cast(int)st.length);
		if (i & 1)
		{
			int u = uniform!"[]"(-100, 100);
			brute[l .. r] += u;
			st.rangeU(l, r, u);
		}
		else
		{
			debug writeln("answer is ", st.rangeF(l, r));
			assert(brute[l .. r].sum == st.rangeF(l, r));
		}
	}
}

version(all) void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new Tuple!(int, int, int)[](n);
	foreach(i, ref segi; segs)
	{
		segi[1] = readInt!int - 1;
		segi[2] = readInt!int - 1;
		segi[0] = readInt!int;
	}
	sort(segs);
	auto st = STL!(long, long.max, long, min, (mn, u, sz) => mn + u, (nu, ou) => nu + ou)(m - 1, 0);
	alias add = i => st.rangeU(segs[i][1], segs[i][2], 1);
	alias remove = i => st.rangeU(segs[i][1], segs[i][2], -1);
	int ans = int.max;
	int i = 0;
	int j = -1;
	while (i < n)
	{
		while (j+1 < n && st.rangeF(0, m - 1) == 0) add(++j);
		if (st.rangeF(0, m - 1)) ans = min(ans, segs[j][0] - segs[i][0]);
		remove(i);
		i++;
	}
	ans.writeln;
}

struct STL(V, V neutralV, U, alias f, alias fou, alias uou)
{
	V[] _nF;
	U[] _nU;
	long[] _nSz;
	bool[] _nHasU;
	size_t length = 0;
	int layers = 0;
	this(size_t n, V v)
	{
		n = cast(size_t)nextPow2(cast(long)n);
		auto fn = n;
		while (fn) {layers++; fn >>= 1;}
		length = n;
		auto noNodes = 2 * n;
		_nF = new V[](noNodes);
		_nF[] = neutralV;
		_nF[n .. 2 * n] = v;
		foreach_reverse(i; 1 .. n) _nF[i] = f(_nF[i<<1], _nF[(i<<1)+1]);
		_nU = new U[](noNodes);
		_nHasU = new bool[](noNodes);
		_nHasU[] = false;
		_nSz = new long[](noNodes);
		_nSz[n .. 2 * n] = 1;
		foreach_reverse(i; 1 .. n) _nSz[i] = _nSz[i<<1] * 2;
	}
	void _clearU(size_t l, size_t r)
	{
		pragma(inline, true);
		r--;
		auto li = layers - 1;
		for(; li > 0 && (l >> li) == (r >> li); li--) _clearU(l);
		for(; li > 0; li--) _clearU(l >> li), _clearU(r >> li);
	}
	void _refresh(size_t l, size_t r)
	{
		pragma(inline, true);
		r--;
		l >>= 1; r >>= 1;
		for(; l != r; l >>= 1, r >>= 1)
		{
			_nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
			_nF[r] = f(_getNodeF(r<<1), _getNodeF((r<<1)+1));
		}
		for(; l; l >>= 1) _nF[l] = f(_getNodeF(l<<1), _getNodeF((l<<1)+1));
	}
	V rangeF(size_t l, size_t r)
	{
		l += length, r += length;
		_clearU(l, r);
		V value;
		void delegate(V) add;
		void delegate(V) combine = (V v) { value = f(value, v); };
		void delegate(V) set = (V v) { value = v; add = combine; };
		add = set;
		_onComps!(c => set(_getNodeF(c)))(l, r);
		return value;
	}
	void rangeU(size_t l, size_t r, U u)
	{
		l += length, r += length;
		_onComps!(c => _addU(c, u))(l, r);
		_refresh(l, r);
	}
	V _getNodeF(size_t i)
	{
		pragma(inline, true);
		if (_nHasU[i]) return cast(V) fou(_nF[i], _nU[i], _nSz[i]);
		return _nF[i];
	}
	bool _isLeaf(size_t i)
	{
		pragma(inline, true);
		return i >= length;
	}
	void _clearU(size_t i)
	{
		pragma(inline, true);
		if (!_nHasU[i]) return;
		if (_isLeaf(i))
		{
			_nF[i] = _getNodeF(i);
			_nHasU[i] = false;
			return;
		}
		_nHasU[i] = false;
		_addU(i << 1, _nU[i]), _addU((i << 1) + 1, _nU[i]);
		_nF[i] = f(_getNodeF(i<<1), _getNodeF((i<<1)+1));
	}
	void _addU(size_t i, U u)
	{
		pragma(inline, true);
		if (!_nHasU[i])
		{
			_nU[i] = u;
			_nHasU[i] = true;
			return;
		}
		_nU[i] = uou(u, _nU[i]);
	}
}
void _onComps(alias act)(size_t l, size_t r)
{
	for(; l < r; l >>= 1, r >>= 1)
	{
		if (l & 1) act(l++);
		if (r & 1) act(--r);
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}

import std.stdio;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto segs = new int[2][](n);
	auto segPerW = new int[][](1_000_000 + 1);
	auto maxW = long.min;
	foreach(i, ref segi; segs)
	{
		segi[0] = readInt!int - 1;
		segi[1] = readInt!int - 1;
		auto w = readInt!long;
		maxW = max(maxW, w);
		segPerW[cast(int)w] ~= cast(int)i;
	}
// struct SegmentTreeLazy(V, U, alias f, alias fou, alias uou, string fName = null, string uName = null)
	bool can(int diff)
	{
		auto st = SegmentTreeLazy!(long, long,
				(a, b) => min(a, b),
				(mn, u, i, f) => mn + u,
				(u1, u2) => u1 + u2,
				"min",
				"+")(m, 0);
		debug writeln("dif =  ", diff);
		foreach(i; 0 .. diff)
			foreach(segi; segPerW[i])
			{
				st[segs[segi][0] .. segs[segi][1]] += 1;
			}
		debug
		{
			foreach(i; 0 .. m)
				st[i .. i + 1].min.write(" ");
			writeln;
		}

		foreach(i; diff .. 1_000_000 + 1)
		{
			foreach(seg; segPerW[i])
			{
				debug writeln("adding segment = ", segs[seg][0] + 1, " ",
						segs[seg][1] + 1);
				st.rangeU(segs[seg][0], segs[seg][1] - 1, 1);
			}
			debug
			{
				foreach(j; 0 .. m)
					st[j .. j + 1].min.write(" ");
				writeln;
			}
			if (st.rangeF(0, m - 1) > 0) return true;
			foreach(seg; segPerW[i - diff])
			{
				debug writeln("removing segment = ", segs[seg][0] + 1, " ",
						segs[seg][1] + 1);
				st.rangeU(segs[seg][0], segs[seg][1] - 1, -1);
			}
			debug
			{
				foreach(j; 0 .. m)
					st[j .. j + 1].min.write(" ");
				writeln;
			}
		}
		return false;
	}
	int hi = 1_000_000;
	int lo = 0;
	int ans = -1;
	while (lo <= hi)
	{
		auto mid = (lo + hi) / 2;
		if (can(mid))
		{
			ans = mid;
			hi = mid - 1;
		}
		else
		{
			lo = mid + 1;
		}
	}
	ans.writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

import std.algorithm;
import std.random;

struct SegmentTreeLazy(V, U, alias f, alias fou, alias uou, string fName = null, string uName = null)
{
	alias This = typeof(this);
	size_t length;
	size_t realLength = 0;
	V[] _nodeFValue;
	U[] _nodeUpdate;
	bool[] _nodeHasUpdate;
	size_t[] _nodeRangeEnd;
	size_t[] _nodeRangeStart;
	string toString()
	{
		import std.conv;
		string str;
		foreach(i; 0 .. realLength)
			if (_nodeHasUpdate[i])
			{
				str ~= text(_nodeFValue[i], "(", _nodeUpdate[i], "), ");
			}
			else
			{
				str ~= text(_nodeFValue[i], ", ");
			}
		return str;
	}
	this(size_t length, V initialValue)
	{
		this.length = length;
		size_t noNodes = 4 * length;
		_nodeFValue = new V[](noNodes);
		_nodeUpdate = new U[](noNodes);
		_nodeHasUpdate = new bool[](noNodes);
		_nodeRangeEnd = new size_t[](noNodes);
		_nodeRangeStart = new size_t[](noNodes);
		void fill(size_t i, size_t rangeStart, size_t rangeEnd)
		{
			realLength = max(realLength, i + 1);
			_nodeRangeStart[i] = rangeStart;
			_nodeRangeEnd[i] = rangeEnd;
			if (_isLeaf(i)) { _nodeFValue[i] = initialValue; return; }
			size_t l = i<<1;
			size_t r = l+1;
			size_t m = (rangeStart + rangeEnd) / 2;
			fill(l, rangeStart, m), fill(r, m + 1, rangeEnd);
			_nodeFValue[i] = f(_nodeFValue[l], _nodeFValue[r]);
		}
		fill(1, 0, length - 1);
	}
	V rangeF(size_t start, size_t end)
	{
		bool set = false;
		V result;
		void visit(size_t i) in(_hasFragments(i, start, end))
		{
			if (_isFragment(i, start, end))
			{
				if (!set) { result = _cookedF(i); set = true; }
				else { result = f(result, _cookedF(i)); }
				return;
			}
			_clearUpdate(i);
			size_t l = i<<1, r = (i<<1)+1;
			if (_hasFragments(l, start, end)) visit(l);
			if (_hasFragments(r, start, end)) visit(r);
		}
		visit(1);
		return result;
	}
	void rangeU(size_t start, size_t end, U u)
	{
		void visit(size_t i) in(_hasFragments(i, start, end))
		{
			if (_isFragment(i, start, end))
			{
				_addUpdate(i, u);
				return;
			}
			_clearUpdate(i);
			size_t l = i<<1, r = (i<<1)+1;
			if (_hasFragments(l, start, end)) visit(l);
			if (_hasFragments(r, start, end)) visit(r);
			_nodeFValue[i] = f(_cookedF(l), _cookedF(r));
		}
		visit(1);
	}
	V _cookedF(size_t i)
	{
		if (_nodeHasUpdate[i]) { return fou(_nodeFValue[i],
						    _nodeUpdate[i],
						    _nodeRangeStart[i],
						    _nodeRangeEnd[i]); }
		return _nodeFValue[i];
	}
	void _clearUpdate(size_t i)
	{
		if (!_nodeHasUpdate[i]) return;
		if (_isLeaf(i)) { _nodeFValue[i] = _cookedF(i); _nodeHasUpdate[i] = false; return;}
		size_t l = i<<1, r = (i<<1)+1;
		_addUpdate(l, _nodeUpdate[i]), _addUpdate(r, _nodeUpdate[i]);
		_nodeFValue[i] = f(_cookedF(l), _cookedF(r));
		_nodeHasUpdate[i] = false;
	}
	void _addUpdate(size_t i, U u)
	{
		if (!_nodeHasUpdate[i]) { _nodeHasUpdate[i] = true; _nodeUpdate[i] = u; return; }
		_nodeUpdate[i] = uou(_nodeUpdate[i], u);
	}
	bool _isLeaf(size_t i)
	{
		return _nodeRangeStart[i] == _nodeRangeEnd[i];
	}
	bool _isFragment(size_t i, size_t rangeStart, size_t rangeEnd)
	{
		return rangeStart <= _nodeRangeStart[i] && _nodeRangeEnd[i] <= rangeEnd;
	}
	bool _hasFragments(size_t i, size_t rangeStart, size_t rangeEnd)
	{
		return _nodeRangeStart[i] <= rangeEnd && rangeStart <= _nodeRangeEnd[i];
	}
	// prettifiers
	struct Handle
	{
		import std.ascii;
		size_t _start, _end;
		This* _parent;
		auto f()
		{
			return _parent.rangeF(_start, _end);
		}
		auto u(U v)
		{
			_parent.rangeU(_start, _end, v);
		}
		static if (fName !is null)
		{
			mixin(q{alias }, fName, q{ = f;});
		}
		static if (uName !is null)
		{
			static if (uName[0].isAlphaNum)
			{
				mixin(q{alias }, uName, q{ = u;});
			}
			else static if (uName[0] != ' ')
			{
				ref Handle opOpAssign(string operator)(U v)
				if (operator == uName)
				{
					u(v);
					return this;
				}
			}
			else
			{
				ref Handle opAssign(U v)
				{
					u(v);
					return this;
				}
			}
		}
	}
	size_t[2] opSlice(size_t i)(size_t s, size_t e) if (i == 0)
	{
		return [s, e-1];
	}
	auto opIndex(size_t[2] slice)
	{
		return Handle(slice[0], slice[1], &this);
	}
}


import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main(string[ ] args) {
    immutable int N = ceil(sqrt((10 ^^ 9).to!double)).to!int;

    int[] ps;
    bool[N] isPr; isPr[] = true;
    for (int i = 2; i < N; ++i) {
        if (!isPr[i]) { continue; }
        ps ~= i;
        for (int j  = i + i; j < N; j += i) { isPr[j] = false; }
    }

    debug { ps.length.writeln; }

    int t;
    readf!"%s"(t);
    readln;

    while (t--) {
        int a, b, k;
        readf!"%s %s %s"(a, b, k);
        readln;

        if (k == 1) {
            writeln(a != b && (a % b == 0 || b % a == 0) ? "YES" : "NO");
            continue;
        }

        int countDivs(int x) {
            int res = 0;
            foreach (p; ps) {
                if (p > x / p) { break; }

                while (x % p == 0) {
                    ++res;
                    x /= p;
                }
            }

            if (x > 1) { ++res; }

            return res;
        }

        int resa = countDivs(a);
        int resb = countDivs(b);

        auto mx = resa + resb;

        writeln(k <= mx ? "YES" : "NO");
    }
}

// code by cutx64

module main;

import std.string, std.array, std.container, std.typecons;
import std.stdio, std.format, std.file;
import std.algorithm, std.conv, std.functional, std.range, core.bitop;
// import std.bigint, std.complex, std.bitmanip;
// import std.math, std.mathspecial, std.numeric;

void main() {

  immutable PRIME_MAX = 10 ^^ 6;
  int[] primes;
  bool[] isprime = new bool[PRIME_MAX];
  isprime[] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i < PRIME_MAX; ++i) {
    if (isprime[i]) {
      primes ~= i;
      for (int j = 2; i * j < PRIME_MAX; ++j)
        isprime[j * i] = false;
    }
  }
  int[] decomposition(int x) {
    int[] f;
    foreach(p; primes) {
      if (p * p > x) break;
      while (x % p == 0) {
        f ~= p;
        x /= p;
      }
    }
    if (x != 1) f ~= x;
    return f;
  }

  int tests;
  readf!"%s\n"(tests);
  foreach(per_test; 0 .. tests) {
    int x, y, k;
    readf!"%s %s %s\n"(x, y, k);
    int[] fx = decomposition(x);
    int[] fy = decomposition(y);
    if (k > fx.length + fy.length) "NO".writeln;
    else if (k >= 2 || (k == 1 && (x % y == 0 || y % x == 0) && x != y)) "YES".writeln;
    else "NO".writeln;
  }

} // main

import std.stdio;
import std.math;
import std.numeric;
import std.functional;
import std.container;
import std.typecons;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import core.bitop;
import core.stdc.stdio : scanf;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

void main() {
    int T = read!int;
    auto primes = primesUnder(1_000_000);
    void solve() {
        long a, b, k; readf("%d %d %d\n", &a, &b, &k);
        long minC, maxC;
        if (a != b && (a % b == 0 || b % a == 0)) {
            minC = 1;
        } else {
            minC = 2;
        }
        auto fa = factor(a, primes);
        auto fb = factor(b, primes);
        maxC = fa.values.sum + fb.values.sum;
        writeln(minC <= k && k <= maxC ? "YES" : "NO");
    }
    foreach (t; 0 .. T) solve();

}

long[] primesUnder(int M) {
    auto isPrime = new bool[M+1]; {
        isPrime[] = true;
        isPrime[0] = isPrime[1] = false;
    }
    long[] primes;
    for (int x = 2; x <= M; x++) {
        if (isPrime[x]) {
            primes ~= x;
            for (int y = x+x; y <= M; y += x) {
                isPrime[y] = false;
            }
        }
    }
    return primes;
}

long[long] factor(long x, long[] primes) {
    long[long] ret;
    foreach (p; primes) {
        if (p * p > x) break;
        int c = 0;
        while (x % p == 0) {
            c++;
            x /= p;
        }
        if (c > 0) {
            ret[p] = c;
        }
    }
    if (x > 1) {
        ret[x] = 1;
    }
    return ret;
}

import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main(string[ ] args) {
    immutable int N = 10 ^^ 5;

    int[] ps;
    bool[N] isPr; isPr[] = true;
    for (int i = 2; i < N; ++i) {
        if (!isPr[i]) { continue; }
        ps ~= i;
        for (int j  = i + i; j < N; j += i) { isPr[j] = false; }
    }

    debug { ps.length.writeln; }

    int t;
    readf!"%s"(t);
    readln;

    while (t--) {
        int a, b, k;
        readf!"%s %s %s"(a, b, k);
        readln;

        if (k == 1) {
            writeln(a != b && (a % b == 0 || b % a == 0) ? "YES" : "NO");
            continue;
        }

        int countDivs(int x) {
            int res = 0;
            foreach (p; ps) {
                if (p > x / p) { break; }

                while (x % p == 0) {
                    ++res;
                    x /= p;
                }
            }

            if (x > 1) { ++res; }

            return res;
        }

        int resa = countDivs(a);
        int resb = countDivs(b);

        auto mx = resa + resb;

        writeln(k <= mx ? "YES" : "NO");
    }
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main(string[ ] args) {
    immutable int N = ceil(sqrt((10 ^^ 9).to!double)).to!int;

    int[] ps;
    bool[N] isPr; isPr[] = true;
    for (int i = 2; i < N; ++i) {
        if (!isPr[i]) { continue; }
        ps ~= i;
        for (int j  = i + i; j < N; j += i) { isPr[j] = false; }
    }

    debug { ps.length.writeln; }

    int t;
    readf!"%s"(t);
    readln;

    while (t--) {
        int a, b, k;
        readf!"%s %s %s"(a, b, k);
        readln;

        if (k == 1) {
            writeln(a != b && (a % b == 0 || b % a == 0) ? "YES" : "NO");
            continue;
        }

        int countDivs(int x) {
            int res = 0;
            foreach (p; ps) {
                // if (p > x / p) { break; }

                while (x % p == 0) {
                    ++res;
                    x /= p;
                }
            }

            if (x > 1) { ++res; }

            return res;
        }

        int resa = countDivs(a);
        int resb = countDivs(b);

        auto mx = resa + resb;

        writeln(k <= mx ? "YES" : "NO");
    }
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main(string[ ] args) {
    immutable int N = 10 ^^ 6;

    int[] ps;
    bool[N] isPr; isPr[] = true;
    for (int i = 2; i < N; ++i) {
        if (!isPr[i]) { continue; }
        ps ~= i;
        for (int j  = i + i; j < N; j += i) { isPr[j] = false; }
    }

    int t;
    readf!"%s"(t);
    readln;

    while (t--) {
        int a, b, k;
        readf!"%s %s %s"(a, b, k);
        readln;

        if (k == 1) {
            writeln(a != b && (a % b == 0 || b % a == 0) ? "YES" : "NO");
            continue;
        }

        int countDivs(int x) {
            int res = 0;
            foreach (p; ps) {
                if (p > x / p) { break; }

                while (x % p == 0) {
                    ++res;
                    x /= p;
                }
            }

            if (x > 1) { ++res; }

            return res;
        }

        int resa = countDivs(a);
        int resb = countDivs(b);

        auto mx = resa + resb;

        writeln(k <= mx ? "YES" : "NO");
    }
}

// code by cutx64

module main;

import std.string, std.array, std.container, std.typecons;
import std.stdio, std.format, std.file;
import std.algorithm, std.conv, std.functional, std.range, core.bitop;
// import std.bigint, std.complex, std.bitmanip;
// import std.math, std.mathspecial, std.numeric;

void main() {

  immutable PRIME_MAX = 10 ^^ 6;
  int[] primes;
  bool[] isprime = new bool[PRIME_MAX];
  isprime[] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i < PRIME_MAX; ++i) {
    if (isprime[i]) {
      primes ~= i;
      for (int j = 2; i * j < PRIME_MAX; ++j)
        isprime[j] = false;
    }
  }
  int[] decomposition(int x) {
    int[] f;
    foreach(p; primes) {
      if (p * p > x) break;
      while (x % p == 0) {
        f ~= p;
        x /= p;
      }
    }
    if (x != 1) f ~= x;
    return f;
  }

  int tests;
  readf!"%s\n"(tests);
  foreach(per_test; 0 .. tests) {
    int x, y, k;
    readf!"%s %s %s\n"(x, y, k);
    int[] fx = decomposition(x);
    int[] fy = decomposition(y);
    if (k > fx.length + fy.length) "NO".writeln;
    else if (k >= 2 || (k == 1 && (x % y == 0 || y % x == 0) && x != y)) "YES".writeln;
    else "NO".writeln;
  }

} // main


import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

real solve (int n)
{
	auto alpha = PI / n;
	real x = 0.0;
	real y = 0.0;
	real phi = 0.0;
	foreach (i; 0..n)
	{
		x += cos (phi);
		y += sin (phi);
		phi += alpha;
	}
	return cos (alpha / 2) * hypot (y, x);
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		readln.strip.to !(int).solve.writefln !("%.20f");
	}
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto n = readln.chomp.to!double * 2;
        auto t = 180 * (n - 2) / 2 / n * PI / 180.0;
        writefln("%.12f", 0.5 * tan(t) * 2);
    }
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	double PI = 3.14159265359;
	auto t = RD!int;
	auto ans = new double[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto rad = PI / n;
		ans[ti] = 1.0;
		foreach (i; 0..n/2-1)
		{
			ans[ti] += cos(rad*(i+1)) * 2;
		}
	}

	foreach (e; ans)
	{
		writefln(FMT_F, e);
	}
	stdout.flush;
	debug readln;
}

import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

real solve (int n)
{
	auto alpha = PI / n;
	writeln (alpha * 180.0 / PI);
	real x = 0.0;
	real y = 0.0;
	real phi = 0.0;
	foreach (i; 0..n)
	{
		x += cos (phi);
		y += sin (phi);
		phi += alpha;
	}
	return cos (alpha / 2) * hypot (y, x);
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		readln.strip.to !(int).solve.writefln !("%.20f");
	}
}