#include <bits/stdc++.h>
using namespace std;
vector<pair<int, pair<int, int> > > v;
int la[4000005], seg[4000005];
void cal(int lv, int st, int ed) {
  int lv2 = lv * 2;
  if (st != ed) {
    la[lv2] += la[lv];
    la[lv2 + 1] += la[lv];
  }
  seg[lv] += la[lv];
  la[lv] = 0;
}
void up(int st, int ed, int lv, int l, int r, int val) {
  cal(lv, st, ed);
  if (st > r || ed < l) {
    return;
  }
  if (st >= l && ed <= r) {
    la[lv] += val;
    cal(lv, st, ed);
    return;
  }
  int lv2 = lv * 2;
  int md = (st + ed) / 2;
  up(st, md, lv2, l, r, val);
  up(md + 1, ed, lv2 + 1, l, r, val);
  seg[lv] = min(seg[lv2], seg[lv2 + 1]);
}
int main() {
  int t, p;
  scanf("%d%d", &t, &p);
  p--;
  for (int i = 0; i < t; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    b--;
    v.push_back({c, {a, b}});
  }
  sort(v.begin(), v.end());
  int mn = 1e7;
  int po = 0;
  for (int i = 0; i < t; i++) {
    up(1, p, 1, v[i].second.first, v[i].second.second, 1);
    while (seg[1] > 0) {
      up(1, p, 1, v[po].second.first, v[po].second.second, -1);
      mn = min(mn, v[i].first - v[po].first);
      po++;
    }
  }
  printf("%d\n", mn);
}
