#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e6+5;
const int ff = 0x3f3f3f3f;
struct node
{
	int o;
	int e;
	int l;
	int r;
	int d;
	ll cost;
};

int n,x;
struct node a[maxn];
ll sum[maxn];

bool cmp(struct node p,struct node q)
{
	if(p.e!= q.e)
		return p.e< q.e;
	else
		return p.o< q.o;
}

int main()
{
	cin>>n>>x;

	int l,r,c;
	int cnt = 0;
	for(int i = 1;i<= n;i++)
	{
		scanf("%d %d %d",&l,&r,&c);
		a[++cnt].o = 1;
		a[cnt].r = r;
		a[cnt].l = l;
		a[cnt].e = l;
		a[cnt].cost = c;
		a[cnt].d = r-l+1;
		a[++cnt].o = 2;
		a[cnt].l = l;
		a[cnt].r = r;
		a[cnt].e = r;
		a[cnt].cost = c;
		a[cnt].d = r-l+1;
	}

	sort(a+1,a+cnt+1,cmp);
	for(int i = 1;i<= 2e5+1;i++)
		sum[i] = 1e15;

	ll ans = 1e15;
	for(int i = 1;i<= cnt;i++)
	{
		if(a[i].o == 1)
		{
			if(x - (a[i].d)> 0)
			{
				ans = min(sum[x - a[i].d]+a[i].cost,ans);
			}
		}
		else
		{
			sum[a[i].d] = min(sum[a[i].d],a[i].cost);
		}
	}

	if(ans >= 1e15)
		cout<<-1<<endl;
	else
		cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e6+5;
const int ff = 0x3f3f3f3f;
struct node
{
	int o;
	int e;
	int l;
	int r;
	int d;
	ll cost;
};

int n,x;
struct node a[maxn];
ll sum[maxn];

bool cmp(struct node p,struct node q)
{
	if(p.e!= q.e)
		return p.e< q.e;
	else
		return p.o< q.o;
}

int main()
{
	cin>>n>>x;

	int l,r,c;
	int cnt = 0;
	for(int i = 1;i<= n;i++)
	{
		scanf("%d %d %d",&l,&r,&c);
		a[++cnt].o = 1;
		a[cnt].r = r;
		a[cnt].l = l;
		a[cnt].e = l;
		a[cnt].cost = c;
		a[cnt].d = r-l+1;
		a[++cnt].o = 2;
		a[cnt].l = l;
		a[cnt].r = r;
		a[cnt].e = r;
		a[cnt].cost = c;
		a[cnt].d = r-l+1;
	}

	sort(a+1,a+n+1,cmp);
	for(int i = 1;i<= 2e5+1;i++)
		sum[i] = 1e15;

	ll ans = 1e15;
	for(int i = 1;i<= cnt;i++)
	{
		if(a[i].o == 1)
		{
			if(x - (a[i].d)> 0)
			{
				ans = min(sum[x - a[i].d]+a[i].cost,ans);
			}
		}
		else
		{
			sum[a[i].d] = min(sum[a[i].d],a[i].cost);
		}
	}

	if(ans >= 1e15)
		cout<<-1<<endl;
	else
		cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e6+5;
const int ff = 0x3f3f3f3f;
struct node
{
	int o;
	int e;
	int l;
	int r;
	ll cost;
};

int n,x;
struct node a[maxn];
ll sum[maxn];

bool cmp(struct node p,struct node q)
{
	return p.e< q.e;
}

int main()
{
	cin>>n>>x;

	int l,r,c;
	int cnt = 0;
	for(int i = 1;i<= n;i++)
	{
		scanf("%d %d %d",&l,&r,&c);
		a[++cnt].o = 1;
		a[cnt].r = r;
		a[cnt].l = l;
		a[cnt].e = l;
		a[cnt].cost = c;
		a[++cnt].o = 2;
		a[cnt].l = l;
		a[cnt].r = r;
		a[cnt].e = r;
		a[cnt].cost = c;
	}

	sort(a+1,a+n+1,cmp);
	for(int i = 1;i<= 2e5+1;i++)
		sum[i] = 1e15;

	ll ans = 1e16;
	for(int i = 1;i<= cnt;i++)
	{
		if(a[i].o == 1)
		{
			if(x - (a[i].r-a[i].l+1)>= 0&&sum[x - (a[i].r-a[i].l+1)]+a[i].cost< ans)
				ans = sum[x - (a[i].r-a[i].l+1)]+a[i].cost;
		}
		else
		{
			sum[a[i].r - a[i].l+1] = min(sum[a[i].r - a[i].l+1],a[i].cost);
		}
	}

	if(ans  == 1e16)
		cout<<-1<<endl;
	else
		cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e6+5;
const int ff = 0x3f3f3f3f;
struct node
{
	int o;
	int e;
	int l;
	int r;
	ll cost;
};

int n,x;
struct node a[maxn];
ll sum[maxn];

bool cmp(struct node p,struct node q)
{
	return p.e< q.e;
}

int main()
{
	cin>>n>>x;

	int l,r,c;
	int cnt = 0;
	for(int i = 1;i<= n;i++)
	{
		scanf("%d %d %d",&l,&r,&c);
		a[++cnt].o = 1;
		a[cnt].r = r;
		a[cnt].l = l;
		a[cnt].e = l;
		a[cnt].cost = c;
		a[++cnt].o = 2;
		a[cnt].l = l;
		a[cnt].r = r;
		a[cnt].e = r;
		a[cnt].cost = c;
	}

	sort(a+1,a+n+1,cmp);
	for(int i = 1;i<= 2e5+1;i++)
		sum[i] = 1e15;

	ll ans = 1e16;
	for(int i = 1;i<= cnt;i++)
	{
		if(a[i].o == 1)
		{
			if(x - (a[i].r-a[i].l+1)>= 0)
			{
				ans = min(sum[x - (a[i].r-a[i].l+1)]+a[i].cost,ans);
			}
		}
		else
		{
			sum[a[i].r - a[i].l+1] = min(sum[a[i].r - a[i].l+1],a[i].cost);
		}
	}

	if(ans >= 1e15)
		cout<<-1<<endl;
	else
		cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll inf = 1e15;
struct node{
   int i,l,sign;
   ll cost;
   bool operator < (const node nd)const
   {
   		if(i!= nd.i)
       return i < nd.i;
    	else
    	return sign< nd.sign;
   }
};
node a[500005];
ll dp[510000];
int main()
{
    int n,x;
    cin >> n >> x;
    int k = 0;
    for(int i = 0; i < n; i++)
    {
        int l,r;
        ll c;
        scanf("%d%d%I64d",&l,&r,&c);
        a[k].i = l;
        a[k].l = r-l+1;
        a[k].sign = 1;
        a[k++].cost = c;
        a[k].i = r;
        a[k].l = r-l+1;
        a[k].sign = 2;
        a[k++].cost = c;
    }
    for(int j = 0; j < 300000; j++)
        dp[j] = inf;
    sort(a,a+k);
    ll max_ = 1e15;
    for(int j = 0; j < k ;j++)
    {
        if(a[j].sign == 2)
        {
            dp[a[j].l] = min(dp[a[j].l],a[j].cost);
        }
        else
        {
            int tmp = x - a[j].l;
            if(tmp >0 && dp[tmp]!=inf)
            {
                max_ = min(max_,a[j].cost+dp[tmp]);
            }
        }
    }
    if(max_ == 1e15)
        cout << "-1"<<endl;
    else
    cout << max_ <<endl;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e6+5;
const int ff = 0x3f3f3f3f;
struct node
{
	int o;
	int e;
	int l;
	int r;
	ll cost;
};

int n,x;
struct node a[maxn];
ll sum[maxn];

bool cmp(struct node p,struct node q)
{
	if(p.e!= q.e)
		return p.e< q.e;
	else
		return p.o< q.o;
}

int main()
{
	cin>>n>>x;

	int l,r,c;
	int cnt = 0;
	for(int i = 1;i<= n;i++)
	{
		scanf("%d %d %d",&l,&r,&c);
		a[++cnt].o = 1;
		a[cnt].r = r;
		a[cnt].l = l;
		a[cnt].e = l;
		a[cnt].cost = c;
		a[++cnt].o = 2;
		a[cnt].l = l;
		a[cnt].r = r;
		a[cnt].e = r;
		a[cnt].cost = c;
	}

	sort(a+1,a+n+1,cmp);
	for(int i = 1;i<= 2e5+1;i++)
		sum[i] = 1e15;

	ll ans = 1e16;
	for(int i = 1;i<= cnt;i++)
	{
		if(a[i].o == 1)
		{
			if(x - (a[i].r-a[i].l+1)> 0)
			{
				ans = min(sum[x - (a[i].r-a[i].l+1)]+a[i].cost,ans);
			}
		}
		else
		{
			sum[a[i].r - a[i].l+1] = min(sum[a[i].r - a[i].l+1],a[i].cost);
		}
	}

	if(ans >= 1e15)
		cout<<-1<<endl;
	else
		cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll inf = 1e15;
struct node{
   int i,l,sign;
   ll cost;
   bool operator < (const node nd)const
   {
       return i < nd.i;
   }
};
node a[500005];
ll dp[510000];
int main()
{
    int n,x;
    cin >> n >> x;
    int k = 0;
    for(int i = 0; i < n; i++)
    {
        int l,r;
        ll c;
        scanf("%d%d%I64d",&l,&r,&c);
        a[k].i = l;
        a[k].l = r-l+1;
        a[k].sign = 1;
        a[k++].cost = c;
        a[k].i = r;
        a[k].l = r-l+1;
        a[k].sign = 2;
        a[k++].cost = c;
    }
    for(int j = 0; j < 300000; j++)
        dp[j] = inf;
    sort(a,a+k);
    ll max_ = 1e15;
    for(int j = 0; j < k ;j++)
    {
        if(a[j].sign == 2)
        {
            dp[a[j].l] = min(dp[a[j].l],a[j].cost);
        }
        else
        {
            int tmp = x - a[j].l;
            if(tmp >0 && dp[tmp]!=inf)
            {
                max_ = min(max_,a[j].cost+dp[tmp]);
            }
        }
    }
    if(max_ == 1e15)
        cout << "-1"<<endl;
    else
    cout << max_ <<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=3E5+100,INF=0X7FFFFFFF;
vector<pair<int, int> > vp[MAXN];
int n,x,l,r,c;

int main()
{

    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r>>c;
        vp[r-l+1].push_back(make_pair(l,c));
    }
    for(int i=0;i<=x;i++)
    {
        sort(vp[i].begin(),vp[i].end());
    }
    int ans=INF;
    for(int d=1;d<x;d++)
    {
       int mn = INF;
        for(int i=0, j=0; i<vp[d].size(); ++i)
        {
            while(j<vp[x-d].size() && vp[x-d][j].first+d-1<vp[d][i].first)
            {
                if(mn > vp[x-d][j].second) mn = vp[x-d][j].second;
                ++j;
            }
            if(mn != INF)
            {
                int s = mn + vp[d][i].second;
                if(ans > s) ans = s;
            }
        }
    }
    if(ans!=INF)
    cout<<ans<<endl;
    else
        cout<<-1<<endl;
    return 0;
}


include <bits/stdc++.h>

using namespace std;

const int MAXN = 3E5 + 100, INF = 0X7FFFFFFF;
int n, x, l, r, c;
vector<pair<int, int>> vp[MAXN];

int main()
{
    scanf("%d%d", &n, &x);
    for(int i=0; i<n; ++i)
    {
        scanf("%d%d%d", &l, &r, &c);
        vp[r-l+1].push_back({l, c});
    }
    for(int i=0; i<=x; ++i) sort(vp[i].begin(), vp[i].end());

    int ans = INF;

    for(int d=1; d<x; ++d)
    {
        int mn = INF;
        auto & u=vp[d], &v = vp[x-d];

        for(int i=0, j=0; i<v.size(); ++i)
        {
            while(j<u.size() && u[j].first+d-1<v[i].first)
            {
                if(mn > u[j].second) mn = u[j].second;
                ++j;
            }
            if(mn != INF)
            {
                int s = mn + v[i].second;
                if(ans > s) ans = s;
            }
        }

    }
    if(ans == INF) cout << -1 << endl;
    else cout << ans << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=3E5+100,INF=0X7FFFFFFF;
vector<pair<int, int> > vp[MAXN];
int n,x,l,r,c;

int main()
{

    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r>>c;
        vp[r-l+1].push_back(make_pair(l,c));
    }
    for(int i=0;i<=x;i++)
    {
        sort(vp[i].begin(),vp[i].end());
    }
    int ans=INF;
    for(int d=1;d<x;d++)
    {
       int mn = INF;
        for(int i=0, j=0; i< vp[x-d].size(); ++i)
        {
            while(j<vp[d].size() && vp[d][j].first+d-1<vp[x-d][i].first)
            {
                if(mn > vp[d][j].second) mn = vp[d][j].second;
                ++j;
            }
            if(mn != INF)
            {
                int s = mn + vp[x-d][i].second;
                if(ans > s) ans = s;
            }
        }
    }
    if(ans!=INF)
    cout<<ans<<endl;
    else
        cout<<-1<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=3E5+100,INF=0X7FFFFFFF;
vector<pair<int, int> > vp[MAXN];
int n,x,l,r,c;

int main()
{

    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r>>c;
        vp[r-l+1].push_back(make_pair(l,c));
    }
    for(int i=0;i<=x;i++)
    {
        sort(vp[i].begin(),vp[i].end());
    }
    int ans=INF;
    for(int d=1;d<=x/2;d++)
    {
        if(vp[d].size()==0) continue;
        for(int i=0;i<vp[d].size();i++)
        {
            for(int j=0;j<vp[x-d].size();j++)
            {
                int rtimed=d-1+vp[d][i].first;
                int rtimexd=x-d-1+vp[x-d][j].first;
                    //cout<<rtimed<<" "<<vp[x-d][j].first<<endl;
                    if(rtimed<vp[x-d][j].first||rtimexd<vp[d][i].first)
                        ans=min(ans,vp[d][i].second+vp[x-d][j].second);
            }
        }
    }
    if(ans!=INF)
    cout<<ans<<endl;
    else
        cout<<-1<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=3E5+100;
vector<pair<int, int> > vp[MAXN];
int n,x,l,r,c;

int main()
{

    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r>>c;
        vp[r-l+1].push_back(make_pair(l,c));
    }
    for(int i=0;i<=x;i++)
    {
        sort(vp[i].begin(),vp[i].end());
    }
    int ans=5e6;
    for(int d=1;d<=x/2;d++)
    {
        if(vp[d].size()==0) continue;
        for(int i=0;i<vp[d].size();i++)
        {
            for(int j=0;j<vp[x-d].size();j++)
            {
                int rtimed=d-1+vp[d][i].first;
                int rtimexd=x-d-1+vp[x-d][j].first;
                    //cout<<rtimed<<" "<<vp[x-d][j].first<<endl;
                    if(rtimed<vp[x-d][j].first||rtimexd<vp[d][i].first)
                        ans=min(ans,vp[d][i].second+vp[x-d][j].second);
            }
        }
    }
    if(ans!=5e6)
    cout<<ans<<endl;
    else
        cout<<-1<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=3E5+100,INF=0X7FFFFFFF;
vector<pair<long, long> > vp[MAXN];
int n,x,l,r,c;

int main()
{

    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r>>c;
        vp[r-l+1].push_back(make_pair(l,c));
    }
    for(int i=0;i<=x;i++)
    {
        sort(vp[i].begin(),vp[i].end());
    }
    int ans=INF;
    for(int d=1;d<=x/2;d++)
    {
        if(vp[d].size()==0) continue;
        for(int i=0;i<vp[d].size();i++)
        {
            for(int j=0;j<vp[x-d].size();j++)
            {
                int rtimed=d-1+vp[d][i].first;
                int rtimexd=x-d-1+vp[x-d][j].first;
                    //cout<<rtimed<<" "<<vp[x-d][j].first<<endl;
                    if(rtimed<vp[x-d][j].first||rtimexd<vp[d][i].first)
                        ans=min(ans,vp[d][i].second+vp[x-d][j].second);
            }
        }
    }
    if(ans!=INF)
    cout<<ans<<endl;
    else
        cout<<-1<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=3E5+100,INF=0X7FFFFFFF;
vector<pair<int, int> > vp[MAXN];
int n,x,l,r,c;

int main()
{

    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r>>c;
        vp[r-l+1].push_back(make_pair(l,c));
    }
    for(int i=0;i<=x;i++)
    {
        sort(vp[i].begin(),vp[i].end());
    }
    int ans=INF;
    for(int d=1;d<=x/2;d++)
    {
        if(vp[d].size()==0||x-d==d) continue;
        for(int i=0;i<vp[d].size();i++)
        {
            for(int j=0;j<vp[x-d].size();j++)
            {
                int rtimed=d-1+vp[d][i].first;
                int rtimexd=x-d-1+vp[x-d][j].first;
                    //cout<<rtimed<<" "<<vp[x-d][j].first<<endl;
                    if(rtimed<vp[x-d][j].first||rtimexd<vp[d][i].first)
                        ans=min(ans,vp[d][i].second+vp[x-d][j].second);
            }
        }
    }
    if(ans!=INF)
    cout<<ans<<endl;
    else
        cout<<-1<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=3E5+100,INF=0X7FFFFFFF;
vector<pair<int, int> > vp[MAXN];
int n,x,l,r,c;

int main()
{

    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r>>c;
        vp[r-l+1].push_back(make_pair(l,c));
    }
    for(int i=0;i<=x;i++)
    {
        sort(vp[i].begin(),vp[i].end());
    }
    int ans=INF;
    for(int d=1;d<=x/2;d++)
    {
        if(vp[d].size()==0) continue;
        if(x-d!=d)
        {
        for(int i=0;i<vp[d].size();i++)
        {
            int num=x-d;
            for(int j=0;j<vp[num].size();j++)
            {
                int rtimed=d-1+vp[d][i].first;
                int rtimexd=num-1+vp[num][j].first;
                    if(rtimed<vp[num][j].first||rtimexd<vp[d][i].first)
                        ans=min(ans,vp[d][i].second+vp[num][j].second);
            }
        }
        }
        else{
            for(int i=0;i<vp[d].size();i++)
            {
                int j=0;
                int rtimed=d-1+vp[d][i].first;
                for(int j=i+1;j<vp[d].size();j++)
                {
                    if(rtimed<vp[d][j].first)
                    ans=min(ans,vp[d][i].second+vp[d][j].second);
                }
            }
        }
    }
    if(ans!=INF)
    cout<<ans<<endl;
    else
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define nmax 200005
#define INF 100000000000
using namespace std;

ll n, h, l[nmax], r[nmax], c[nmax];
map<ll,ll> bit[nmax];

void up(ll x,ll val, ll days){
    for (;x<nmax;x+=(x&(-x)))
        if(bit[days].count(x) > 0) bit[days][x] = min(bit[days][x], val);
        else bit[days][x] = val;
}

ll get(ll x, ll y){
    ll res = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y].count(x) > 0) res = min(res, bit[y][x]);
    return res;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(ll i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1 >= h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    ll ans = INF;
    for(ll i = 1; i <= n; i++){
        if(h-(r[i]-l[i]+1) <= 0) continue;
        ans = min(ans, c[i] + get(l[i]-1, h-(r[i]-l[i]+1)) );
    }
    if(ans != INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200005
#define INF 10000000000000000
using namespace std;
int n, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[2*nmax];
void up(int x,int k, int y){
    for (;x<2*nmax;x+=(x&(-x)))
    if(bit[y][x] != 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x]!=0) S=min(S, bit[y][x]);
    if(S < INF) return S;
    else return -1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t != -1) ans = min(ans, c[i] + t);
    }
    if(ans < INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200000+5
using namespace std;
int n, ans, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];
void up(int x,int k, int y){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[y][x] != 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    //if(x == 0 || y == 0) return -1;
    int S=1e15;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x]!=0)
            S=min(S, bit[y][x]);
    if(S!=1e15) return S;
    else return -1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    ans = 1e17;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t!= -1)
            ans = min(ans, c[i] + t);

    }

    if(ans!=1e17) cout << ans;
    else cout << -1;

}


#include<bits/stdc++.h>
#define int long long
#define nmax 200005
using namespace std;
int n, ans, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];
void up(int x,int k, int y){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[y][x] != 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S=1e18;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x]!=0) S=min(S, bit[y][x]);
    if(S!=1e18) return S;
    else return -1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    ans = 1e18;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t!= -1) ans = min(ans, c[i] + t);
    }
    if(ans!=1e18) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200005
#define INF 10000000000000000
using namespace std;
int n, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];
void up(int x,int k, int y){
    for (;x<h;x+=(x&(-x)))
    if(bit[y][x] != 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x]!=0) S=min(S, bit[y][x]);
    if(S < INF) return S;
    else return -1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1>=h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t != -1) ans = min(ans, c[i] + t);
    }
    if(ans < INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200010
#define INF 100000000000000
using namespace std;
int n, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];
void up(int x,int k, int y){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[y].count(x) > 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y].count(x)> 0) S=min(S, bit[y][x]);
    if(S < INF) return S;
    else return -1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1>=h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t != -1) ans = min(ans, c[i] + t);
    }
    if(ans != INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200005
#define INF 10000000000000000
using namespace std;
int n, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];
void up(int x,int k, int y){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[y][x] != 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S = INF;
    bool ok = false;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x]!=0){
            ok = 1;
            S=min(S, bit[y][x]);
        }
    if(ok) return S;
    else return -1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1>=h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    int ans = INF;
    bool ok = false;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t != -1){
            ok = 1;
            ans = min(ans, c[i] + t);
        }
    }
    if(ok) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200010
#define INF 100000000000000
using namespace std;

int n, h, l[nmax], r[nmax], c[nmax];
map<int,int> bit[nmax];

void up(int x,int val, int days){
    for (;x<nmax;x+=(x&(-x))){
        if(bit[days].count(x) > 0) bit[days][x] = min(bit[days][x], val);
        else bit[days][x] = val;
    }
}
int get(int x, int y){
    int S = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y].count(x)> 0) S=min(S, bit[y][x]);
    return S;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1 >= h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        if(h-(r[i]-l[i]+1) <= 0) continue;
        ans = min(ans, c[i] + get(l[i]-1, h-(r[i]-l[i]+1)) );
    }
    if(ans != INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200005
using namespace std;
int n, ans, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];
void up(int x,int k, int y){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[y][x] != 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S=1e18;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x]!=0) S=min(S, bit[y][x]);
    if(S!=1e17) return S;
    else return -1;
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    ans = 1e18;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t!= -1) ans = min(ans, c[i] + t);
    }
    if(ans!=1e18) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200005
#define INF 10000000000000000
using namespace std;
int n, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];
void up(int x,int k, int y){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[y][x] != 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x]!=0) S=min(S, bit[y][x]);
    if(S < INF) return S;
    else return -1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1>=h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t != -1) ans = min(ans, c[i] + t);
    }
    if(ans < INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 400005
#define INF 100000000000000
using namespace std;

int n, h, l[nmax], r[nmax], c[nmax];
map<int,int> bit[nmax];

void up(int x,int val, int days){
    for (;x<nmax;x+=(x&(-x))){
        if(bit[days].count(x) > 0) bit[days][x] = min(bit[days][x], val);
        else bit[days][x] = val;
    }
}

int get(int x, int y){
    int res = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y].count(x) > 0) res = min(res, bit[y][x]);
    return res;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1 >= h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        if(h-(r[i]-l[i]+1) <= 0) continue;
        ans = min(ans, c[i] + get(l[i]-1, h-(r[i]-l[i]+1)) );
    }
    if(ans != INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200010
#define INF 100000000000000
using namespace std;

int n, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];

void up(int x,int k, int y){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[y].count(x) > 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y].count(x)> 0) S=min(S, bit[y][x]);
    if(S < INF) return S;
    else return INF;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1>=h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        ans = min(ans, c[i] + t);
    }
    if(ans != INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define int long long
#define nmax 200005
#define INF 1000000000000000
using namespace std;
int n, ans, l[nmax], h, r[nmax], c[nmax];
map<int,int> bit[nmax];
void up(int x,int k, int y){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[y][x] != 0) bit[y][x] = min(k, bit[y][x]);
    else bit[y][x] = k;
}
int get(int x, int y){
    int S=INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x]!=0) S=min(S, bit[y][x]);
    if(S!=INF) return S;
    else return -1;
}
main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    ans = INF;
    for(int i = 1; i <= n; i++){
        if(h-r[i]+l[i]-1<=0) continue;
        int t = get(l[i]-1,h-r[i]+l[i]-1);
        if(t != -1) ans = min(ans, c[i] + t);
    }
    if(ans < INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
#define ll long long
#define nmax 200010
#define INF 100000000000
using namespace std;

ll n, h, l[nmax], r[nmax], c[nmax];
map<ll,ll> bit[nmax];

void up(ll x,ll val, ll days){
    for (;x<nmax;x+=(x&(-x)))
    if(bit[days][x] > 0) bit[days][x] = min(bit[days][x], val);
        else bit[days][x] = val;
}

ll get(ll x, ll y){
    ll res = INF;
    for (;x>0;x-=(x&(-x)))
        if(bit[y][x] > 0) res = min(res, bit[y][x]);
    return res;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> h;
    for(ll i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> c[i];
        if(r[i]-l[i]+1 >= h) continue;
        up(r[i]+1, c[i], r[i]-l[i]+1);
    }
    ll ans = INF;
    for(ll i = 1; i <= n; i++){
        if(h-(r[i]-l[i]+1) <= 0) continue;
        ans = min(ans, c[i] + get(l[i], h-(r[i]-l[i]+1) ) );
    }
    if(ans != INF) cout << ans;
    else cout << -1;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e9 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        kq = min(kq, query(1, 1, n, max(L[kent] , i + 1) , start[i] - 1) + a[i].val);
        kq = min(kq, query(1, 1, n, finish[i] + 1, R[kent]) + a[i].val);
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e9 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        kq = min(kq, query(1, 1, n, max(L[kent] , i + 1) , start[i] - 1) + a[i].val);
        kq = min(kq, query(1, 1, n, max(finish[i] + 1 , i + 1) , R[kent]) + a[i].val);
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e9 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 3 ; i <= 3 ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
            kq = min(kq, query(1, 1, n, max(L[kent] , i + 1) , start[i] - 1) + a[i].val);
            kq = min(kq, query(1, 1, n, finish[i] + 1, R[kent]) + a[i].val);
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e9 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        if (L[kent] > i)
        {
            kq = min(kq, query(1, 1, n, L[kent], start[i] - 1) + a[i].val);
            kq = min(kq, query(1, 1, n, finish[i] + 1, R[kent]) + a[i].val);
        }
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e9 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    freopen("a.inp","r",stdin);
    freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        kq = min(kq, query(1, 1, n, max(L[kent] , i + 1) , start[i] - 1) + a[i].val);
        kq = min(kq, query(1, 1, n, finish[i] + 1, R[kent]) + a[i].val);
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e9 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

int main()
{
    freopen("a.inp","r",stdin);
    freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 5 ; i <= n ; i++)
    {
        int l = i, r = n + 1, mid;
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1) start[i] = r;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r = n + 1, mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        if (L[kent] > i)
        {
            kq = min(kq, query(1, 1, n, L[kent], start[i] - 1) + a[i].val);
            kq = min(kq, query(1, 1, n, finish[i] + 1, R[kent]) + a[i].val);
        }
        cout << i << " " << start[i] << " " << finish[i] << endl;
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e18 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        kq = min(kq, query(1, 1, n, max(L[kent] , i + 1) , start[i] - 1) + a[i].val);
        kq = min(kq, query(1, 1, n, max(finish[i] + 1 , i + 1) , R[kent]) + a[i].val);
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e18 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1e18 + 7;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        kq = min(kq, query(1, 1, n, max(L[kent] , i + 1) , start[i] - 1) + a[i].val);
        kq = min(kq, query(1, 1, n, max(finish[i] + 1 , i + 1) , R[kent]) + a[i].val);
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e18 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e18 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1e18 + 7;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        l = i , r = R[a[i].second - a[i].first + 1] + 1 , mid;
        while(r - l > 1){
            mid = (l + r) / 2;
            if (a[mid].first <= a[i].second) l = mid;
            else r = mid;
        }
        kq = min(kq, query(1, 1, n, max(L[kent] , r) , start[i] - 1) + a[i].val);
        kq = min(kq, query(1, 1, n, max(finish[i] + 1 , r) , R[kent]) + a[i].val);
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
        //cout << kq << endl;
    }
    if (kq == 1e18 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e18 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        kq = min(kq, query(1, 1, n, max(L[kent] , i + 1) , start[i] - 1) + a[i].val);
        kq = min(kq, query(1, 1, n, max(finish[i] + 1 , i + 1) , R[kent]) + a[i].val);
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e18 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e9 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

int main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        int l = i, r = n + 1, mid;
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1) start[i] = r;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != a[i].second - a[i].first) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r = n + 1, mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        if (L[kent] > i)
        {
            kq = min(kq, query(1, 1, n, L[kent], start[i] - 1) + a[i].val);
            kq = min(kq, query(1, 1, n, finish[i] + 1, R[kent]) + a[i].val);
        }
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 7;

struct han
{
    int first;
    int second;
    int val;
} a[MAXN];

bool cmp(han X, han Y)
{
    int val1 = X.second - X.first;
    int val2 = Y.second - Y.first;
    if (val1 == val2)
    {
        return X.first < Y.first;
    }
    return val1 < val2;
}
int n, x, kent, woodburn, brand, start[MAXN], finish[MAXN], L[MAXN], R[MAXN], kq = 1e9 + 7, T[4 * MAXN];
bool check(int vt)
{
    int gt = a[vt].second - a[vt].first + 1;
    if (gt < kent) return 1;
    if (gt == kent)
    {
        if (a[vt].second < brand || woodburn < a[vt].first) return 1;
    }
    return 0;
}
bool check2(int vt)
{
    int handeptrai = a[vt].second - a[vt].first + 1;
    if (handeptrai == kent){
    if (a[vt].second < brand) return 0;
    if (woodburn < a[vt].first) return 0;
    }
    if (handeptrai != kent) return 0;
    return 1;
}

int query(int node, int ll, int rr, int u, int v)
{
    if (v<ll or rr<u or ll>rr)
    {
        return 1000111000;
    }
    if (u<=ll and ll<=rr and rr<=v) return T[node];
    return min(
               query(node*2, ll, (ll+rr)/2, u, v),
               query(node*2+1, (ll+rr)/2+1, rr, u, v)
           );
}

void update(int node, int ll, int rr, int i, int v)
{
    if (i<ll or i>rr or ll>rr) return;
    if (ll!=rr)
    {
        update(node*2, ll, (ll+rr)/2, i, v);
        update(node*2+1, (ll+rr)/2+1, rr, i, v);
        T[node] = min(T[node*2], T[node*2+1]);
    }
    else T[node] = v;
}

int main()
{
    //freopen("a.inp","r",stdin);
    //freopen("a.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> x;
    for(int i = 1 ; i <= n ; i++)
    {
        cin >> a[i].first >> a[i].second >> a[i].val;
    }
    sort(a + 1, a + n + 1, cmp);
    /*
    for(int i = 1 ; i <= n ; i++)
        cout << a[i].first << " " << a[i].second << " " << a[i].val << endl;
    cout << endl;
    */
    for(int i = 1 ; i <= n ; i++)
        update(1, 1, n, i, a[i].val);
    for(int i = 1 ; i <= n ; i++)
    {
        int kt = a[i].second - a[i].first + 1;
        if (L[kt] == 0) L[kt] = i;
        R[kt] = i;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        kent = x - (a[i].second - a[i].first + 1);
        brand = a[i].first;
        woodburn = a[i].second;
        if (kent <= 0) continue;
        int l = max(i + 1 , L[kent]) - 1;
        int r = R[kent] + 1;
        int mid;
        while(r - l > 1)
        {
            mid = (l + r) / 2;
            if (check(mid)) l = mid;
            else r = mid;
        }
        if (r > i && r <= n + 1)
        start[i] = r;
        if (start[i] == R[kent] + 1) start[i]--;
        //cout << start[i] << endl;
        if (start[i] > i && start[i] <= n && a[start[i]].second - a[start[i]].first != x - a[i].second + a[i].first - 2) continue;
        if (start[i] > 0)
        {
            l = start[i] - 1, r =  R[kent] + 1 , mid;
            while(r - l > 1)
            {
                mid = (l + r) / 2;
                if (check2(mid)) l = mid;
                else r = mid;
            }
            finish[i] = l;
        }
        if (finish[i] == 0) finish[i] = -1;
        if (L[kent] > i)
        {
            kq = min(kq, query(1, 1, n, L[kent], start[i] - 1) + a[i].val);
            kq = min(kq, query(1, 1, n, finish[i] + 1, R[kent]) + a[i].val);
        }
        //cout << i << " " << L[kent] << " " << start[i] << " " << finish[i] << " " << R[kent] << endl;
    }
    if (kq == 1e9 + 7)
    {
        cout << -1;
        return 0;
    }
    cout << kq;
    return 0;
}


#include <bits/stdc++.h>
#define maxn 200009
#define Task ""
#define vt vector
#define pb push_back
#define pii pair <int, int>
#define mp make_pair
#define F first
#define S second
#define ll long long
#define mod 10000000000000007

using namespace std;
int n, x;
long long f[maxn];

struct vacation
{
    long long l, r, cost;

    bool operator < (const vacation &b) const
    {
        return r > b.r;
    }

}a[maxn];
priority_queue <vacation> q;

void nhap()
{
    scanf("%d %d ", &n, &x);
    for (int i = 1; i <= n; i++)
        scanf ("%I64d %I64d %I64d", &a[i].l, &a[i].r, &a[i].cost);
}

bool cmp (vacation a, vacation b)
{
    if (a.l != b.l) return a.l < b.l;
    if (a.r != b.r) return a.r < b.r;
    return a.cost < b.cost;
}

void solve()
{
    long long ans = mod;
    sort (a+1, a+n+1, cmp);
    for (int i = 1; i <= x; i++)
        f[i] = mod;
    for (int i = 1; i <= n; i++)
    {
        long long len = a[i].r - a[i].l + 1;
        if (len >= x) continue;
        while (!q.empty() && q.top().r < a[i].l)
        {
            vacation p = q.top(); q.pop();
            int sz = p.r - p.l + 1;
            f[sz] = min(f[sz], p.cost);
        }
        q.push(a[i]);
        if (f[x-len] == mod) continue;
        ans = min(ans, f[x-len] + a[i].cost);
    }
    if (ans == mod) cout << -1;
    else cout << ans;
}

int main()
{
    nhap();
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define maxn 200009
#define Task ""
#define vt vector
#define pb push_back
#define pii pair <int, int>
#define mp make_pair
#define F first
#define S second
#define ll long long
#define mod 1000000007

using namespace std;
int n, x;
int f[maxn];

struct vacation
{
    int l, r, cost;

    bool operator < (const vacation &b) const
    {
        return r > b.r;
    }

}a[maxn];
priority_queue <vacation> q;

void nhap()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++)
        cin >> a[i].l >> a[i].r >> a[i].cost;
}

bool cmp (vacation a, vacation b)
{
    if (a.l != b.l) return a.l < b.l;
    if (a.r != b.r) return a.r < b.r;
    return a.cost < b.cost;
}

void solve()
{
    int ans = mod;
    sort (a+1, a+n+1, cmp);
    for (int i = 1; i <= x; i++)
        f[i] = mod;
    for (int i = 1; i <= n; i++)
    {
        int len = a[i].r - a[i].l + 1;
        while (!q.empty() && q.top().r < a[i].l)
        {
            vacation p = q.top(); q.pop();
            int sz = p.r - p.l + 1;
            f[sz] = min(f[sz], p.cost);
        }
        q.push(a[i]);
        if (f[x-len] == mod) continue;
        ans = min(ans, f[x-len] + a[i].cost);
    }
    if (ans == mod) cout << -1;
    else cout << ans;
}

int main()
{
    nhap();
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define maxn 200009
#define Task ""
#define vt vector
#define pb push_back
#define pii pair <int, int>
#define mp make_pair
#define F first
#define S second
#define ll long long
#define mod 1000000007

using namespace std;
int n, x;
int f[maxn];

struct vacation
{
    int l, r, cost;

    bool operator < (const vacation &b) const
    {
        return r > b.r;
    }

}a[maxn];
priority_queue <vacation> q;

void nhap()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++)
        cin >> a[i].l >> a[i].r >> a[i].cost;
}

bool cmp (vacation a, vacation b)
{
    if (a.l != b.l) return a.l < b.l;
    if (a.r != b.r) return a.r < b.r;
    return a.cost < b.cost;
}

void solve()
{
    int ans = mod;
    sort (a+1, a+n+1, cmp);
    for (int i = 1; i <= x; i++)
        f[i] = mod;
    for (int i = 1; i <= n; i++)
    {
        int len = a[i].r - a[i].l + 1;
        while (!q.empty() && q.top().r < a[i].l)
        {
            vacation p = q.top(); q.pop();
            int sz = p.r - p.l + 1;
            f[sz] = min(f[sz], p.cost);
        }
        q.push(a[i]);
        if (f[x-len] == mod) continue;
        ans = min(ans, f[x-len] + a[i].cost);
    }
    cout << ans;
}

int main()
{
    nhap();
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define maxn 200009
#define Task ""
#define vt vector
#define pb push_back
#define pii pair <int, int>
#define mp make_pair
#define F first
#define S second
#define ll long long
#define mod 10000000000000007

using namespace std;
int n, x;
long long f[maxn];

struct vacation
{
    long long l, r, cost;

    bool operator < (const vacation &b) const
    {
        return r > b.r;
    }

}a[maxn];
priority_queue <vacation> q;

void nhap()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++)
        cin >> a[i].l >> a[i].r >> a[i].cost;
}

bool cmp (vacation a, vacation b)
{
    if (a.l != b.l) return a.l < b.l;
    if (a.r != b.r) return a.r < b.r;
    return a.cost < b.cost;
}

void solve()
{
    long long ans = mod;
    sort (a+1, a+n+1, cmp);
    for (int i = 1; i <= x; i++)
        f[i] = mod;
    for (int i = 1; i <= n; i++)
    {
        long long len = a[i].r - a[i].l + 1;
        if (len >= x) continue;
        while (!q.empty() && q.top().r < a[i].l)
        {
            vacation p = q.top(); q.pop();
            int sz = p.r - p.l + 1;
            f[sz] = min(f[sz], p.cost);
        }
        q.push(a[i]);
        if (f[x-len] == mod) continue;
        ans = min(ans, f[x-len] + a[i].cost);
    }
    if (ans == mod) cout << -1;
    else cout << ans;
}

int main()
{
    nhap();
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define maxn 200009
#define Task ""
#define vt vector
#define pb push_back
#define pii pair <int, int>
#define mp make_pair
#define F first
#define S second
#define ll long long
#define mod 1000000007

using namespace std;
int n, x;
int f[maxn];

struct vacation
{
    int l, r, cost;

    bool operator < (const vacation &b) const
    {
        return r > b.r;
    }

}a[maxn];
priority_queue <vacation> q;

void nhap()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++)
        cin >> a[i].l >> a[i].r >> a[i].cost;
}

bool cmp (vacation a, vacation b)
{
    if (a.l != b.l) return a.l < b.l;
    if (a.r != b.r) return a.r < b.r;
    return a.cost < b.cost;
}

void solve()
{
    int ans = mod;
    sort (a+1, a+n+1, cmp);
    for (int i = 1; i <= x; i++)
        f[i] = mod;
    for (int i = 1; i <= n; i++)
    {
        int len = a[i].r - a[i].l + 1;
        if (len >= x) continue;
        while (!q.empty() && q.top().r < a[i].l)
        {
            vacation p = q.top(); q.pop();
            int sz = p.r - p.l + 1;
            f[sz] = min(f[sz], p.cost);
        }
        q.push(a[i]);
        if (f[x-len] == mod) continue;
        ans = min(ans, f[x-len] + a[i].cost);
    }
    if (ans == mod) cout << -1;
    else cout << ans;
}

int main()
{
    nhap();
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define maxn 200009
#define Task ""
#define vt vector
#define pb push_back
#define pii pair <int, int>
#define mp make_pair
#define F first
#define S second
#define ll long long
#define mod 1000000007

using namespace std;
int n, x;
int f[maxn];

struct vacation
{
    int l, r, cost;

    bool operator < (const vacation &b) const
    {
        return r > b.r;
    }

}a[maxn];
priority_queue <vacation> q;

void nhap()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++)
        cin >> a[i].l >> a[i].r >> a[i].cost;
}

bool cmp (vacation a, vacation b)
{
    if (a.l != b.l) return a.l < b.l;
    if (a.r != b.r) return a.r < b.r;
    return a.cost < b.cost;
}

void solve()
{
    int ans = mod;
    sort (a+1, a+n+1, cmp);
    for (int i = 1; i <= x; i++)
        f[i] = mod;
    for (int i = 1; i <= n; i++)
    {
        int len = a[i].r - a[i].l + 1;
        if (len > x) continue;
        while (!q.empty() && q.top().r < a[i].l)
        {
            vacation p = q.top(); q.pop();
            int sz = p.r - p.l + 1;
            f[sz] = min(f[sz], p.cost);
        }
        q.push(a[i]);
        if (f[x-len] == mod) continue;
        ans = min(ans, f[x-len] + a[i].cost);
    }
    if (ans == mod) cout << -1;
    else cout << ans;
}

int main()
{
    nhap();
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
vector <pair<long long int,long long int> > interval[2*100010];

int main()
{
	long long int n,x,i,j,k,minans=10000000000,l,r,c;
	cin>>n>>x;
	for (i=0;i<n;i++)
	{
		cin>>l>>r>>c;
		interval[r-l+1].pb(mp(l,c));
	}
	for (i=0;i<=x;i++)	sort(interval[i].begin(),interval[i].end());
	for (i=1;i<x;i++)
	{
		for (j=0;j<interval[x-i].size();j++)
		{
			for (k=0;k<interval[i].size() && interval[i][k].first+i-1<interval[x-i][j].first;k++)
			{
				minans = min(interval[i][k].second + interval[x-i][j].second,minans);
			}
		}
	}
	if (minans == 10000000000)	minans=-1;
	cout<<minans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
vector <pair<long long int,long long int> > interval[2*100010];

int main()
{
	ios_base::sync_with_stdio(0);
    cin.tie(0);
	long long int n,x,i,j,k,minans=10000000000,l,r,c;
	cin>>n>>x;
	for (i=0;i<n;i++)
	{
		cin>>l>>r>>c;
		interval[r-l+1].pb(mp(l,c));
	}
	for (i=0;i<=x;i++)	sort(interval[i].begin(),interval[i].end());
	for (i=1;i<x;i++)
	{
		for (j=0;j<interval[x-i].size();j++)
		{
			for (k=0;k<interval[i].size() && interval[i][k].first+i-1<interval[x-i][j].first;k++)
			{
				minans = min(interval[i][k].second + interval[x-i][j].second,minans);
			}
		}
	}
	if (minans == 10000000000)	minans=-1;
	cout<<minans;
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <stack>
using namespace std;

int main() {
    int n;
    cin>>n;
    vector<int> hills;
    int max_h = 0, max_i = 0;
    for (int i = 0; i < n; i++) {
        int h;
        cin>>h;
        hills.push_back(h);
        if (h > max_h) {
            max_h = h;
            max_i = i;
        }
    }

    vector<int> linearHills;
    for (int i = max_i; i < n; i++) {
        linearHills.push_back(hills[i]);
    }

    for (int i = 0; i < max_i; i++) {
        linearHills.push_back(hills[i]);
    }

    stack<pair<int, int>> stackHills;
    int c = 0;
    for (int h : linearHills) {
        if (stackHills.empty()) {
            stackHills.push({h, 1});
            continue;
        }

        while (h > stackHills.top().first) {
            c += stackHills.top().second;
            stackHills.pop();
        }

        if (h == stackHills.top().first) {
            int s = stackHills.top().second;
            c += s;
            if (h != max_h) c++;
            stackHills.pop();
            stackHills.push({h, s + 1});
        } else {
            stackHills.push({h, 1});
            c++;
        }
    }

    while (stackHills.size() > 2) {
        c += stackHills.top().second;
        stackHills.pop();
    }

    if (stackHills.size() == 2) {
        int s = stackHills.top().second;
        stackHills.pop();
        if (stackHills.top().second > 1) c += s;
    }

    cout<<c<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2000000+10;
int n,a[maxn];long long ans,now;

struct State
{
	int val,id;
}q[maxn];

inline int getans(int val,int L,int R)
{
	int l=L,r=R,mid,ans=0;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(q[mid].val==val) ans=mid,r=mid-1;
		else l=mid+1;
	}
	return R-ans+1;
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	int fir=0,sec=0,cnt1=0,cnt2=0;
	for(int i=1;i<=n;i++)
	{
		if(fir<a[i]) fir=a[i],cnt1=0;
		if(fir==a[i]) cnt1++;
	}
	if(cnt1==n) return printf("%I64d\n",1ll*n*(n-1)/2),0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]==fir) continue;
		if(sec<a[i]) sec=a[i],cnt2=0;
		if(sec==a[i]) cnt2++;
	}
	ans=2*(((cnt1==1)?1:2)*cnt2+2*(n-cnt1-cnt2));
	for(int i=1;i<=n;i++) a[i+n]=a[i];
	int l=1,r=0;
	for(int i=1;i<2*n;i++)
	{
		while(l<=r&&q[l].id<=i-n) l++;
		while(l<=r&&q[r].val<a[i]) r--;
		if(i>=n&&l<=r&&q[r].val==a[i]) ((a[i]==fir)?now:ans)+=getans(a[i],l,r);
		q[++r]=(State){a[i],i};
	}
	l=1,r=0;
	for(int i=2*n;i>1;i--)
	{
		while(l<=r&&q[l].id>=i+n) l++;
		while(l<=r&&q[r].val<a[i]) r--;
		if(i<=n+1&&l<=r&&q[r].val==a[i]) ((a[i]==fir)?now:ans)+=getans(a[i],l,r);
		q[++r]=(State){a[i],i};
	}
	printf("%I64d\n",ans/2+now/4);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
const int MAXN=1e6+50;
typedef long long ll;
struct mt
{
    ll high,num;
    mt(){high=0;num=1;}
};
ll n,ans,a[MAXN],v[MAXN],imax,pos;
stack<mt> S;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        imax=max(a[i],imax);
    }
    for(int i=1;i<=n;i++)
        if(a[i]==imax)
        {
            pos=i;
            break;
        }
    int pos2=pos,pos3=pos;
    for(int i=1;i<n;i++)
    {
        if(pos2==n) pos2=0;
        mt x;
        x.high=a[++pos2];
        if(S.empty())   S.push(x);
        else
        {
            if(x.high>=S.top().high)
            {
                if(x.high>=S.top().high)
                {
                while(!S.empty()&&x.high>S.top().high)  ans+=S.top().num,S.pop();
                if(!S.empty()&&x.high==S.top().high)    ans+=S.top().num,S.top().num+=x.num;
                else S.push(x);
                if(S.size()!=1) ans++;
                }
            }
            else S.push(x),ans++;
        }
    }
    int max=0;
    for(int i=1;i<n;i++)
    {
        if(pos3==n) pos3=0;
        int x=a[++pos3];
        if(x>=max)  v[pos3]=1,max=x;
    }
    max=0;
    for(int i=1;i<n;i++)
    {
        if(pos==1)  pos=n+1;
        int x=a[--pos];
        if(x>=max)  v[pos]=1,max=x;
    }
    for(int i=1;i<=n;i++)
        if(v[i])    ans++;
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#define int long long
#define maxn 1000005
#define inf 2147483647
#define mod 10003
#define eps 1e-6
#define pi acos(-1.0)
#define de(x) ((x)*(x))
using namespace std;
inline int read(){
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)) {x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int a[maxn],b[maxn],l[maxn],r[maxn],c[maxn];
int mid,maxx=-1,ans;
signed main(){
	int n=read();
	for(int i=0;i<n;i++){
		a[i]=read();
		if(a[i]>maxx){
			maxx=a[i];
			mid=i;
		}
	}
	mid--;
	for(int i=1;i<=n;i++) b[i]=a[(mid+i)%n];
	l[1]=1;
	for(int i=2;i<=n;i++){
		l[i]=i-1;
		while(l[i]>1&&b[l[i]]<=b[i]) l[i]=l[l[i]];
	}
	for(int i=n;i>=1;i--){
		r[i]=i+1;
		while(r[i]<=n&&b[r[i]]<=b[i]) r[i]=r[r[i]];
		if(r[i]<=n&&b[r[i]]==b[i]){
			c[i]=c[r[i]]+1;
			r[i]=r[r[i]];
		}
	}
	for(int i=2;i<=n;i++){
		ans+=c[i]+2;
		if(l[i]==1&&r[i]==n+1) ans--;
	}
	printf("%lld",ans);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define maxn 1000005
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;

__int64 num1[maxn], num2[maxn], n, ans;
int l[maxn], r[maxn], v[maxn];

int main(int argc, char *argv[])
{
	int flag, maxs, w;
	while(~scanf("%I64d", &n))
	{
		maxs = -1;
		w = 0;
		ans = 0;
		mem(v, 0);
		for(int i = 0;i < n;i++)
		{
			scanf("%I64d", &num1[i]);
			if(maxs < num1[i])
			{
				maxs = num1[i];
				flag = i;
			}
		}
		for(int i = flag;i < n;i++)
		num2[w++] = num1[i];
		for(int i = 0;i < flag;i++)
		num2[w++] = num1[i];
		num2[w] = maxs;
		for(int i = 1;i < n;i++)
		{
			l[i] = i - 1;
			while(l[i]&&num2[i] >= num2[l[i]])
			l[i] = l[l[i]];
		}
		for(int i = (n - 1);i >= 0;i--)
		{
			r[i] = i + 1;
			while(r[i] != n&&num2[i] > num2[r[i]])
			r[i] = r[r[i]];
			if(num2[i] == num2[r[i]]&&r[i] != n)
			{
				v[i] = v[r[i]] + 1;
				r[i] = r[r[i]];
			}
		}
		for(int i = 1;i < n;i++)
		{
			ans += v[i];
			ans += 2;
			if(!l[i]&&r[i] == n)
			ans--;
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    vector<int>height(n);
    for(int i=0;i<n;i++)
	{
		scanf("%d",&(height[i]));
	}
    vector<int>::iterator highest=max_element(height.begin(),height.end());
    vector<int>num(highest,height.end());
    num.reserve(n);
    copy(height.begin(),highest,num.end());
    vector<int>left(n,-1);
    for(int i=1;i<n;i++)
	{
        int p=i-1;
        while(num[i]>num[p])
		{
			p=left[p];
		}
        left[i]=num[i]==num[p]?left[p]:p;
    }
    vector<int>right(n,n);
    vector<int>same(n,0);
    for (int i=n-2;i>=0;i--)
	{
        int p=i+1;
        while(p<n&&num[i]>num[p])
		{
			p=right[p];
		}
        if(p>=n)
		{
            right[i]=p;
            continue;
        }

        right[i]=num[i]==num[p]?right[p]:p;
        same[i]=num[i]==num[p]?same[p]+1:0;
    }
    long long ans=0;
    for(int i=0;i<n;i++)
	{
        if (left[i]>=0)ans+=1;
        if (right[i]<n)ans+=1;
        if (right[i]>=n&&left[i]>0)ans+=1;
        ans+=same[i];
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
#define maxn 1000010
using namespace std;
typedef long long ll;
inline void write(ll x) {
	if (x < 0)putchar('-'), x = -x;
	if (x > 9)write(x / 10);
	putchar(x % 10 + 48);
}
inline int read() {
	int k = 0, f = 1;
	char c = getchar();
	while (c < '0' || c>'9') {
		if (c == '-')f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		k = (k << 1) + (k << 3) + c - 48;
		c = getchar();
	}
	return k * f;
}
int n;
int a[maxn],left[maxn],right[maxn],same[maxn];
ll ans;
stack<int>s;
int main() {
	while (scanf("%d", &n) != EOF) {
		memset(a, 0, sizeof(a));
		memset(left, 0, sizeof(left));
		memset(right, 0, sizeof(right));
		memset(same, 0, sizeof(same));
		for (int i = 0; i < n; ++i)a[i] = read();
		rotate(a, max_element(a, a + n), a + n);
		a[n] = a[0];
		while (!s.empty())s.pop();
		s.push(0);
		left[0] = -1;
		for (int i = 1; i < n; ++i) {
			while (!s.empty() && a[s.top()] <= a[i]) {
				if (a[s.top()] == a[i])same[i] = same[s.top()] + 1;
				s.pop();
			}
			if (s.empty())left[i] = -1;
			else left[i] = s.top();
			s.push(i);
		}
		while (!s.empty())s.pop();
		s.push(n); right[n] = n;
		for (int i = n - 1; i >= 1; --i) {
			while (!s.empty() && a[s.top()] <= a[i]) s.pop();
			if (s.empty())right[i] = n + 1;
			else right[i] = s.top();
			s.push(i);
		}
		ans = 0;
		for (int i = 1; i < n; ++i) {
			if (left[i] != -1)++ans;
			if (right[i] != n + 1)++ans;
			if (left[i] == 0 && right[i] == n)--ans;
			ans += same[i];
		}
		write(ans), putchar('\n');

	}
}


#include <bits/stdc++.h>
#define maxn 1000086
using namespace std;

typedef long long ll;

int n;
int a[maxn], b[maxn], cnt;
int id;
ll ans;
int sk[maxn], top;
int l[maxn], r[maxn], num[maxn];

int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> a[i];
		if(a[i] > a[id]) id = i;
	}
	for(int i = id;i <= n;i++) b[++cnt] = a[i];
	for(int i = 1;i <= id;i++) b[++cnt] = a[i];
	memcpy(a, b, sizeof(b));
	++n;
	for(int i = 1;i <= n;i++){
		while(top && a[sk[top]] <= a[i]) --top;
		if(top) l[i] = sk[top];
		sk[++top] = i;
	}
	top = 0;
	for(int i = n;i;i--){
		while(top && a[sk[top]] < a[i]) --top;
		if(top){
			if(a[i] == a[sk[top]]) num[i] = num[sk[top]] + 1, r[i] = r[sk[top]];
			else r[i] = sk[top];
		}
		sk[++top] = i;
	}
	for(int i = 1;i <= n;i++){
		if(l[i]) ++ans;
		if(r[i]) ++ans;
		if(l[i] == 1 && r[i] == n) ans--;
		ans += num[i];
	}
	printf("%lld", ans - num[1]);
}


#include <bits/stdc++.h>
#define maxn 1000086
using namespace std;

typedef long long ll;

int n;
int a[maxn], b[maxn], cnt;
int id;
ll ans;
int sk[maxn], top;
int l[maxn], r[maxn], num[maxn];

int main(){
	scanf("%d", &n);
	for(int i = 1;i <= n;i++){
		scanf("%d", &a[i]);
		if(a[i] > a[id]) id = i;
	}
	for(int i = id;i <= n;i++) b[++cnt] = a[i];
	for(int i = 1;i <= id;i++) b[++cnt] = a[i];
	memcpy(a, b, sizeof(b));
	++n;
	for(int i = 1;i <= n;i++){
		while(top && a[sk[top]] <= a[i]) --top;
		if(top) l[i] = sk[top];
		sk[++top] = i;
	}
	top = 0;
	for(int i = n;i;i--){
		while(top && a[sk[top]] < a[i]) --top;
		if(top){
			if(a[i] == a[sk[top]]) num[i] = num[sk[top]] + 1, r[i] = r[sk[top]];
			else r[i] = sk[top];
		}
		sk[++top] = i;
	}
	for(int i = 1;i <= n;i++){
		if(l[i]) ++ans;
		if(r[i]) ++ans;
		if(l[i] == 1 && r[i] == n) ans--;
		ans += num[i];
	}
	printf("%lld", ans - num[1]);
}


#include<cstdio>
using namespace std;
int n,a[1000005],id,b[1000005],l[1000005],r[1000005],same[1000005];
long long ans;
int main(){
	scanf ("%d",&n);id=1;
	for (register int i=1;i<=n;++i) scanf ("%d",&a[i]);
	for (register int i=2;i<=n;++i) if (a[i]>a[id]) id=i;
	for (register int i=1;i<id;++i) b[n-id+1+i]=a[i];
	for (register int i=id;i<=n;++i) b[i-id+1]=a[i];
	l[1]=1;
	for (register int i=2;i<=n;++i){
		l[i]=i-1;
		while (l[i]>1&&b[i]>=b[l[i]]) l[i]=l[l[i]];
	}
	for (register int i=n;i>=1;--i){
		r[i]=i+1;
		while (r[i]<=n&&b[i]>b[r[i]]) r[i]=r[r[i]];
		if (r[i]<=n&&b[i]==b[r[i]]){same[i]=same[r[i]]+1;r[i]=r[r[i]];}
	}
	for (register int i=2;i<=n;++i) ans+=same[i]+((l[i]==1&&r[i]==n+1)?1:2);
	printf ("%lld\n",ans);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define LL long long
#include<algorithm>
#include<vector>
#include <iostream>
#define LL long long
using namespace std;
const int maxn=1000005;
const int inf=1<<30;

int Stack[maxn];
int     A[maxn];
int     B[maxn];
LL      f[maxn];
int n;
int main(){
//freopen("C:\\Users\\deii\\Desktop\\test.txt","r",stdin);
long long ans=0;
scanf("%d",&n);int Max=1;
for(int i=1;i<=n;i++) {scanf("%d",&A[i]);if(A[Max]<A[i]) Max=i;}
for(int i=1;i<=n;i++) B[i]=A[(n+Max-2+i)%n+1];
int top=0;
Stack[top++]=1;

for(int i=2;i<=n;i++){
while(top>0&&B[Stack[top-1]]<B[i]) {ans++;top--;}
while(top>0&&B[Stack[top-1]]==B[i]) ans++;
Stack[top++]=i;
ans++;
//printf("i=%d ans=%d\n",i,ans);
}

printf("%I64d\n",ans);
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define LL long long
#include<algorithm>
#include<vector>
#include <iostream>
#include<string.h>
#define LL long long
using namespace std;
const int maxn=1000005;
const int inf=1<<30;

int Stack[maxn];
int     A[maxn];
int     B[maxn];
LL      f[maxn];
int times[maxn];

int n;
int main(){
//freopen("C:\\Users\\deii\\Desktop\\test.txt","r",stdin);
long long ans=0;
scanf("%d",&n);int Max=1;
for(int i=1;i<=n;i++) {scanf("%d",&A[i]);if(A[Max]<A[i]) Max=i;}
for(int i=1;i<=n;i++) {B[i]=A[(n+Max-2+i)%n+1]; times[i]=1;}
int top=0;
Stack[top++]=1;
for(int i=2;i<=n;i++){
while(top>0&&B[Stack[top-1]]<B[i]) {ans+=times[Stack[top-1]];top--;}
if(top>0&&B[Stack[top-1]]==B[i]){ans+=times[Stack[top-1]];times[Stack[top-1]]++;if(top>1) ans++;}
else {Stack[top++]=i;ans++;}

}

Max=-inf;  for(int i=2;i<=n;i++) if(B[i]>Max) {ans--;Max=B[i];}
Max=-inf;  for(int i=n;i>=2;i--) if(B[i]>Max) {ans++;Max=B[i];}

printf("%I64d\n",ans);
}


#include<cstdio>
#define rg register
#define ci const int
#define cl const long long int

typedef long long int ll;

namespace IO {
    char buf[90];
}

template<typename T>
inline void qr(T &x) {
    char ch=getchar(),lst=' ';
    while(ch>'9'||ch<'0') lst=ch,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    if(lst=='-') x=-x;
}

template<typename T>
inline void qw(T x,const char aft,const bool pt) {
    if(x<0) x=-x,putchar('-');
    int top=0;
    do {
        IO::buf[++top]=x%10+'0';
        x/=10;
    } while(x);
    while(top) putchar(IO::buf[top--]);
    if(pt) putchar(aft);
}

template<typename T>
inline T mmax(const T a,const T b) {return a > b ? a : b;}
template<typename T>
inline T mmin(const T a,const T b) {return a < b ? a : b;}
template<typename T>
inline T mabs(const T a) {return a < 0 ? -a : a;}

template<typename T>
inline void mswap(T &a,T &b) {
    T temp=a;a=b;b=temp;
}

const int maxn = 1000010;

struct M {
    int a,b;
};
M stack[maxn];

int n,top;
int MU[maxn],CU[maxn];
bool used[maxn];

int main() {
    qr(n);
    for(rg int i=1;i<=n;++i) qr(MU[i]);
    rg int mx=0;
    for(rg int i=1;i<=n;++i) if(MU[i] > MU[mx]) mx=i;
    for(rg int i=mx;i<=n;++i) CU[++top]=MU[i];
    for(rg int i=1;i<mx;++i) CU[++top]=MU[i];
    mx=0;
    rg ll ans=0;
    stack[top=1]=(M){CU[1],1};
    for(rg int i=2;i<=n;++i) {
        while(top && (stack[top].a < CU[i])) ans+=stack[top--].b;
        if(stack[top].a == CU[i]) ans+=(stack[top].b++)+(top > 1);
        else {stack[++top]=(M){CU[i],1}; ++ans;}
    }
    mx=0;for(rg int i=2;i<=n;++i) if(CU[i] >= mx) {mx=CU[i];used[i]=true;}
    mx=0;for(rg int i=n;i>1;--i) if(CU[i] >= mx) {if(!used[i]) ++ans;mx=CU[i];}
    qw(ans,'\n',true);
    return 0;
}


/*
题意：有一座座山围成一圈，有各自的高度，当两座山之间没有比它们更高的山时认为这两座山彼此可见，求共有多少组山彼此可见。
*/
#include <bits/stdc++.h>

using namespace std;

int hill_c[1000005],hill_l[1000005],lef[1000005],righ[1000005],same[1000005];
// right[i]用来储存i右边第一个比i高的山，left[i]储存i左边第一个比i高的山,same[i]储存(i,r[i]]间与i等高的山

int main()
{
    int n,i,maxh=0,maxi=-1;
    long long ans=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)      //输入
    {
        scanf("%d",&hill_c[i]);
        if(hill_c[i]>maxh)
        {
            maxh=hill_c[i];
            maxi=i;
        }
    }
    for(i=0;i<n;i++)      //将环变成链，取最高的山当作链的头/尾
    {
        hill_l[i]=hill_c[(maxi+i)%n];
    }
    hill_l[i]=hill_c[maxi];
    same[n]=0;
    for(i=n-1;i>=0;i--)      //用单调队列维护r[i]和s[i]
    {
        righ[i]=i+1;
        while(righ[i]<n&&hill_l[i]>hill_l[righ[i]]) righ[i]=righ[righ[i]];
        if(righ[i]<n&&hill_l[i]==hill_l[righ[i]])
        {
            same[i]=same[righ[i]]+1;
            righ[i]=righ[righ[i]];
        }
    }
    for(i=1;i<=n;i++)      //用单调队列维护l[i]
    {
        lef[i]=i-1;
        while(lef[i]>0&&hill_l[i]>=hill_l[lef[i]]) lef[i]=lef[lef[i]];
    }
    for(i=1;i<n;i++)      //计算一共有多少对
    {
        if(lef[i]==0&&righ[i]==n) ans--;
        //if(hill_l[i]==maxh) ans--;
        ans+=same[i]+2;
    }
    printf("%lld",ans);
    return 0;
}
/*
附题解：https://blog.csdn.net/qq_38759433/article/details/86514227
官方题解：http://codeforces.com/blog/entry/213
*/


#include<bits/stdc++.h>
using namespace std;

template <typename T> void chmin(T&x,const T &y)
{
    if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
    if(x<y)x=y;
}
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned short us;
typedef unsigned int ui;
typedef pair<int,int> pii;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define pb push_back
#define mp make_pair
#define gc (c=getchar())
int read()
{
    char c;
    while(gc<'-');
    if(c=='-')
    {
        int x=gc-'0';
        while(gc>='0')x=x*10+c-'0';
        return -x;
    }
    int x=c-'0';
    while(gc>='0')x=x*10+c-'0';
    return x;
}
#undef gc

ll sqr(const ll &x)
{
    return x*x;
}
const int N=1e6+5;
int a[N],a0[N];
pii st[N];int top;
ll solve(int n)
{
    top=1;st[1]=mp(a[1],1);
    ll ans=0;
    rep(i,2,n)
    {
        while(top&&st[top].first<a[i]){ans+=st[top].second;--top;}
        if(st[top].first==a[i])
        {
            ans+=st[top].second+bool(top>1);
            ++st[top].second;
        }
        else
        {
            ++ans;
            st[++top]=mp(a[i],1);
        }
    }
    static bool have[N];
    int mx=0;
    rep(i,2,n)
    {
        if(a[i]>=mx){have[i]=1;mx=a[i];}
    }
    mx=0;
    per(i,n,2)
    if(a[i]>=mx)
    {
        ans+=!have[i];
        mx=a[i];
    }
    return ans;
}
int main()
{
  //  freopen("1.in","r",stdin);//freopen("1.out","w",stdout);
    int n;
    cin>>n;
    rep(i,1,n)a0[i]=read();
    int mxi=1;
    rep(i,2,n)
    if(a0[i]>a0[mxi])mxi=i;
    int top=0;
    rep(i,mxi,n)a[++top]=a0[i];
    rep(i,1,mxi-1)a[++top]=a0[i];
    cout<<solve(n);
}