269_D. Maximum Waterfall

Emuskald was hired to design an artificial waterfall according to the latest trends in landscape architecture. A modern artificial waterfall consists of multiple horizontal panels affixed to a wide flat wall. The water flows down the top of the wall from panel to panel until it reaches the bottom of the wall.

The wall has height t and has n panels on the wall. Each panel is a horizontal segment at height hi which begins at li and ends at ri. The i-th panel connects the points (li, hi) and (ri, hi) of the plane. The top of the wall can be considered a panel connecting the points ( - 109, t) and (109, t). Similarly, the bottom of the wall can be considered a panel connecting the points ( - 109, 0) and (109, 0). No two panels share a common point.

Emuskald knows that for the waterfall to be aesthetically pleasing, it can flow from panel i to panel j (<image>) only if the following conditions hold: 

  1. max(li, lj) < min(ri, rj) (horizontal projections of the panels overlap); 
  2. hj < hi (panel j is below panel i); 
  3. there is no such panel k (hj < hk < hi) that the first two conditions hold for the pairs (i, k) and (k, j). 



Then the flow for <image> is equal to min(ri, rj) - max(li, lj), the length of their horizontal projection overlap.

Emuskald has decided that in his waterfall the water will flow in a single path from top to bottom. If water flows to a panel (except the bottom of the wall), the water will fall further to exactly one lower panel. The total amount of water flow in the waterfall is then defined as the minimum horizontal projection overlap between two consecutive panels in the path of the waterfall. Formally: 

  1. the waterfall consists of a single path of panels <image>; 
  2. the flow of the waterfall is the minimum flow in the path <image>. 



To make a truly great waterfall Emuskald must maximize this water flow, but there are too many panels and he is having a hard time planning his creation. Below is an example of a waterfall Emuskald wants:

<image>

Help Emuskald maintain his reputation and find the value of the maximum possible water flow.

Input

The first line of input contains two space-separated integers n and t (1 ≤ n ≤ 105, 2 ≤ t ≤ 109), the number of the panels excluding the top and the bottom panels, and the height of the wall. Each of the n following lines contain three space-separated integers hi, li and ri (0 < hi < t,  - 109 ≤ li < ri ≤ 109), the height, left and right ends of the i-th panel segment.

It is guaranteed that no two segments share a common point.

Output

Output a single integer — the maximum possible amount of water flow in the desired waterfall.

Examples

Input

5 6
4 1 6
3 2 7
5 9 11
3 10 15
1 13 16


Output

4


Input

6 5
4 2 8
3 1 2
2 2 3
2 6 12
1 0 7
1 8 11


Output

2

Note

The first test case corresponds to the picture.

Example input:
6 5
4 2 8
3 1 2
2 2 3
2 6 12
1 0 7
1 8 11

5 6
4 1 6
3 2 7
5 9 11
3 10 15
1 13 16


Example output:
2

4


Tags: data structures
dp
graphs
sortings
Difficulty rating: 2600

Solution:
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > s;
set<pair<int, int> >::iterator st, it, pre, suc;
int h[120000], l[120000], r[120000], i, j, k, o, n, p, q, P[120000], f[120000];
bool cmp(const int& a, const int& b) { return h[a] > h[b]; }
int main() {
  scanf("%d%*d", &n);
  for (i = 1; i <= n; ++i) scanf("%d%d%d", &h[i], &l[i], &r[i]), P[i] = i;
  h[0] = 1e9;
  l[0] = -2e9;
  r[0] = 2e9;
  f[0] = 2e9;
  h[n + 1] = -1;
  l[n + 1] = -1e9;
  r[n + 1] = 1e9;
  P[n + 1] = n + 1;
  sort(P + 1, P + n + 2, cmp);
  s.insert(make_pair(-1e9, 0));
  s.insert(make_pair(1e9, 0));
  for (o = 1; o <= n + 1; ++o) {
    i = P[o];
    st = s.lower_bound(make_pair(l[i], i));
    for (it = st, it--; it->first < r[i]; ++it) {
      pre = it;
      if (pre != s.begin()) pre--;
      suc = it;
      suc++;
      j = it->second;
      p = pre->second;
      q = suc->second;
      if ((h[j] <= h[p] || l[j] >= r[p] || r[p] <= l[i]) &&
          (h[j] <= h[q] || r[j] <= l[q] || l[q] >= r[i]))
        f[i] = max(f[i], min(f[j], min(r[i], r[j]) - max(l[i], l[j])));
    }
    it--;
    j = it->second;
    s.erase(st, ++it);
    if (r[i] != it->first) s.insert(make_pair(r[i], j));
    s.insert(make_pair(l[i], i));
  }
  printf("%d\n", f[n + 1]);
}