#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int mxn = 1e6 + 7, INF = 1e9;
int n, m;
vector<long long> a, t, lazy;
void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = a[tl];
  } else {
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    t[v] = min(t[v * 2], t[v * 2 + 1]);
  }
}
void push(int v) {
  t[v * 2] += lazy[v];
  lazy[v * 2] += lazy[v];
  t[v * 2 + 1] += lazy[v];
  lazy[v * 2 + 1] += lazy[v];
  lazy[v] = 0;
}
void update(int v, int tl, int tr, int l, int r, int addend) {
  if (l > r) return;
  if (l == tl && tr == r) {
    t[v] += addend;
    lazy[v] += addend;
  } else {
    push(v);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(r, tm), addend);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, addend);
    t[v] = min(t[v * 2], t[v * 2 + 1]);
  }
}
int query(int v, int tl, int tr, int l, int r) {
  if (l > r) {
    return INF;
  }
  if (l <= tl && tr <= r) {
    return t[v];
  }
  push(v);
  int tm = (tl + tr) / 2;
  return min(query(v * 2, tl, tm, l, min(r, tm)),
             query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
void fix() {
  mxn = m + 10;
  a.assign(mxn, 0ll);
  t.assign(4 * mxn, INF);
  lazy.assign(4 * mxn, 0ll);
}
int ans = 1e9;
struct data {
  int l, r, w;
} p[1000000];
bool compare(struct data &d1, struct data &d2) {
  if (d2.w != d1.w) {
    return d2.w > d1.w;
  } else if (d2.l != d1.l) {
    return d2.l > d1.l;
  } else {
    return d2.r > d1.r;
  }
}
void solve() {
  cin >> n >> m;
  fix();
  for (int i = 0; i < n; i++) {
    int l, r, w;
    cin >> l >> r >> w;
    r--;
    p[i].l = l;
    p[i].r = r;
    p[i].w = w;
  }
  sort(p, p + n, compare);
  build(1, 0, m - 1);
  int i = 0, j = -1;
  while (i < n) {
    bool ok = 0;
    while (query(1, 0, m - 1, 1, m - 1) <= 0) {
      j++;
      if (j >= n) {
        ok = 1;
        break;
      }
      update(1, 0, m - 1, p[j].l, p[j].r, 1);
    }
    if (ok) break;
    ans = min(ans, p[j].w - p[i].w);
    update(1, 0, m - 1, p[i].l, p[i].r, -1);
    i++;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
