#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, m;
struct Seg {
  int l, r, w;
  bool operator<(const Seg& t) const { return w < t.w; }
} seg[N];
struct Node {
  int l, r;
  int x, tag;
} tr[N << 2];
void pushup(int u) { tr[u].x = min(tr[u << 1].x, tr[u << 1 | 1].x); }
void pushdown(int u) {
  auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
  int tag = root.tag;
  if (tag) {
    left.x += tag;
    left.tag += tag;
    right.x += tag;
    right.tag += tag;
    root.tag = 0;
  }
}
void build(int u, int l, int r) {
  tr[u] = {l, r, 0, 0};
  if (l == r) return;
  int mid = l + r >> 1;
  build(u << 1, l, mid);
  build(u << 1 | 1, mid + 1, r);
  pushup(u);
}
void add(int u, int l, int r, int x) {
  if (l <= tr[u].l && r >= tr[u].r) {
    tr[u].x += x;
    tr[u].tag += x;
    return;
  }
  int mid = tr[u].l + tr[u].r >> 1;
  pushdown(u);
  if (l <= mid) add(u << 1, l, r, x);
  if (r > mid) add(u << 1 | 1, l, r, x);
  pushup(u);
}
int ask(int u, int l, int r) {
  if (l <= tr[u].l && r >= tr[u].r) return tr[u].x;
  pushdown(u);
  int ans = INT_MAX;
  int mid = tr[u].l + tr[u].r >> 1;
  if (l <= mid) ans = min(ans, ask(u << 1, l, r));
  if (r > mid) ans = min(ans, ask(u << 1 | 1, l, r));
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> seg[i].l >> seg[i].r >> seg[i].w;
    seg[i].r--;
  }
  m--;
  build(1, 1, m);
  sort(seg, seg + n);
  int ans = INT_MAX;
  for (int l = 0, r = 0; r < n; ++r) {
    add(1, seg[r].l, seg[r].r, 1);
    if (ask(1, 1, m) == 0) continue;
    ans = min(ans, seg[r].w - seg[l].w);
    while (l <= r && ask(1, 1, m) > 0) {
      add(1, seg[l].l, seg[l].r, -1);
      ++l;
      if (ask(1, 1, m) > 0) ans = min(ans, seg[r].w - seg[l].w);
    }
  }
  cout << ans;
  return 0;
}

### Prompt

Please formulate a Cpp solution to the following problem:

You are given n segments on a number line, numbered from 1 to n. The i-th segments covers all integer points from l_i to r_i and has a value w_i.

You are asked to select a subset of these segments (possibly, all of them). Once the subset is selected, it's possible to travel between two integer points if there exists a selected segment that covers both of them. A subset is good if it's possible to reach point m starting from point 1 in arbitrary number of moves.

The cost of the subset is the difference between the maximum and the minimum values of segments in it. Find the minimum cost of a good subset.

In every test there exists at least one good subset.

Input

The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5; 2 ≤ m ≤ 10^6) — the number of segments and the number of integer points.

Each of the next n lines contains three integers l_i, r_i and w_i (1 ≤ l_i < r_i ≤ m; 1 ≤ w_i ≤ 10^6) — the description of the i-th segment.

In every test there exists at least one good subset.

Output

Print a single integer — the minimum cost of a good subset.

Examples

Input


5 12
1 5 5
3 4 10
4 10 6
11 12 5
10 12 3


Output


3


Input


1 10
1 10 23


Output


0

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, m;
struct Seg {
  int l, r, w;
  bool operator<(const Seg& t) const { return w < t.w; }
} seg[N];
struct Node {
  int l, r;
  int x, tag;
} tr[N << 2];
void pushup(int u) { tr[u].x = min(tr[u << 1].x, tr[u << 1 | 1].x); }
void pushdown(int u) {
  auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
  int tag = root.tag;
  if (tag) {
    left.x += tag;
    left.tag += tag;
    right.x += tag;
    right.tag += tag;
    root.tag = 0;
  }
}
void build(int u, int l, int r) {
  tr[u] = {l, r, 0, 0};
  if (l == r) return;
  int mid = l + r >> 1;
  build(u << 1, l, mid);
  build(u << 1 | 1, mid + 1, r);
  pushup(u);
}
void add(int u, int l, int r, int x) {
  if (l <= tr[u].l && r >= tr[u].r) {
    tr[u].x += x;
    tr[u].tag += x;
    return;
  }
  int mid = tr[u].l + tr[u].r >> 1;
  pushdown(u);
  if (l <= mid) add(u << 1, l, r, x);
  if (r > mid) add(u << 1 | 1, l, r, x);
  pushup(u);
}
int ask(int u, int l, int r) {
  if (l <= tr[u].l && r >= tr[u].r) return tr[u].x;
  pushdown(u);
  int ans = INT_MAX;
  int mid = tr[u].l + tr[u].r >> 1;
  if (l <= mid) ans = min(ans, ask(u << 1, l, r));
  if (r > mid) ans = min(ans, ask(u << 1 | 1, l, r));
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> seg[i].l >> seg[i].r >> seg[i].w;
    seg[i].r--;
  }
  m--;
  build(1, 1, m);
  sort(seg, seg + n);
  int ans = INT_MAX;
  for (int l = 0, r = 0; r < n; ++r) {
    add(1, seg[r].l, seg[r].r, 1);
    if (ask(1, 1, m) == 0) continue;
    ans = min(ans, seg[r].w - seg[l].w);
    while (l <= r && ask(1, 1, m) > 0) {
      add(1, seg[l].l, seg[l].r, -1);
      ++l;
      if (ask(1, 1, m) > 0) ans = min(ans, seg[r].w - seg[l].w);
    }
  }
  cout << ans;
  return 0;
}

```