// Problem: E. Boring Segments
// Contest: Codeforces - Educational Codeforces Round 112 (Rated for Div. 2)
// URL: http://codeforces.com/contest/1555/problem/E
// Memory Limit: 256 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void out(ll x){
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
inline void write(ll x){
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b)
{
ll res = 1;
while(b)
{
if(b & 1)res = res * a ;
a = a * a ;
b >>= 1;
}
return res;
}
const int inf = 0x3f3f3f3f;
const int maxn=1e6+7,maxm=210000;
int n,m;
struct line{
int l,r;
ll w;
}p[maxn];
bool cmp(line a,line b){
return a.w<b.w;
}
struct node{
int l,r;
ll laz,minn;
}tr[maxn<<2];
void pushup(int u){
tr[u].minn=min(tr[u<<1].minn,tr[u<<1|1].minn);
}
void pushdown(int u){
if(tr[u].laz){
tr[u<<1].laz+=tr[u].laz;
tr[u<<1].minn+=tr[u].laz;
tr[u<<1|1].laz+=tr[u].laz;
tr[u<<1|1].minn+=tr[u].laz;
tr[u].laz=0;
}
}
void build(int u,int l,int r){
tr[u]={l,r,0,0};
if(l==r) return ;
int mid=(l+r)/2;
build(u<<1,l,mid);build(u<<1|1,mid+1,r);
pushup(u);
}
void update(int u,int l,int r,int val){
if(tr[u].l>=l&&tr[u].r<=r){
tr[u].minn+=val;
tr[u].laz+=val;
return ;
}
pushdown(u);
int mid=(tr[u].l+tr[u].r)/2;
if(l<=mid) update(u<<1,l,r,val);
if(r>mid) update(u<<1|1,l,r,val);
pushup(u);
}
ll query(int u,int l,int r){
if(tr[u].l>=l&&tr[u].r<=r){
return tr[u].minn;
}
pushdown(u);
int mid=(tr[u].l+tr[u].r)/2;
ll ans=1e18;
if(l<=mid) ans=min(ans,query(u<<1,l,r));
if(r>mid) ans=min(ans,query(u<<1|1,l,r));
return ans;
}
int main(){
n=read,m=read-1;
rep(i,1,n){
p[i].l=read,p[i].r=read-1,p[i].w=read;
}
sort(p+1,p+1+n,cmp);
build(1,1,m);
int l=1,r=1;
ll ans=1e18;
update(1,p[1].l,p[1].r,1);
while(r<n&&query(1,1,m)==0){
r++;
update(1,p[r].l,p[r].r,1);
}
while(query(1,1,m)>0){
update(1,p[l].l,p[l].r,-1);
l++;
}
ans=min(ans,p[r].w-p[l-1].w);
while(r<n){
while(r<n&&query(1,1,m)==0){
r++;
update(1,p[r].l,p[r].r,1);
}
if(query(1,1,m)==0) break;
while(query(1,1,m)>0){
update(1,p[l].l,p[l].r,-1);
l++;
}
ans=min(ans,p[r].w-p[l-1].w);
}
printf("%lld\n",ans);
return 0;
}