#include<bits/stdc++.h>
#define LL long long int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<LL,LL>
using namespace std;

vector< pair<pii,LL> > v[200005];
vector<pair<LL,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    LL i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(a,c),i));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)

        sort(v[k].begin(),v[k].end());

        for(i=1;i<=200000;i++)
        {
            if(v[i].size())
            {
            for(j=v[i].size()-2;j>=0;j--)
            {
                v[i][j].first.second=min(v[i][j].first.second,v[i][j+1].first .second);
            }
            }
        }

    LL mn=INT_MAX;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size())
        {
           LL pos=upper_bound(v[x-k].begin(),v[x-k].end(),mp(mp(b,-1LL),-1LL))-v[x-k].begin();
           if(pos!=v[x-k].size() && v[x-k][pos].second!=i)
           {
               mn=min(mn,c+v[x-k][pos].first.second);
           }
        }
    }

if(mn==INT_MAX)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<int,int>
using namespace std;

vector< pair<pii,LL> > v[200005];
vector<pair<LL,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    LL i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(a,c),i));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)
         if(v[k].size())
        sort(v[k].begin(),v[k].end());

        for(i=1;i<=200000;i++)
        {
            if(v[i].size())
            {
            for(j=v[i].size()-2;j>=0;j--)
            {
                v[i][j].first.second=min(v[i][j].first.second,v[i][j+1].first .second);
            }
            }
        }

    LL mn=INT_MAX;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size()>0)
        { LL p=0;
           LL pos=upper_bound(v[x-k].begin(),v[x-k].end(),mp(mp(b+1,-1),-1))-v[x-k].begin();
           if(pos<v[x-k].size() && v[x-k][pos].second!=i)
           {
               mn=min(mn,c+v[x-k][pos].first.second);
           }
        }
    }

if(mn==INT_MAX)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<int,int>
using namespace std;

vector< pair<pii,LL> > v[200005];
vector<pair<LL,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    LL i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(a,c),i));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)
         if(v[k].size())
        sort(v[k].begin(),v[k].end());

        for(i=1;i<=200000;i++)
        {
            if(v[i].size())
            {
            for(j=v[i].size()-2;j>=0;j--)
            {
                v[i][j].first.second=min(v[i][j].first.second,v[i][j+1].first .second);
            }
            }
        }

    LL mn=INT_MAX;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size()>0)
        { LL p=0;
           LL pos=upper_bound(v[x-k].begin(),v[x-k].end(),mp(mp(b,-1),-1))-v[x-k].begin();
           if(pos!=v[x-k].size() && v[x-k][pos].second!=i)
           {
               mn=min(mn,c+v[x-k][pos].first.second);
           }
        }
    }

if(mn==INT_MAX)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL long long int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<LL,LL>
using namespace std;

vector<pair<pii,pii> > v[200005];
vector<pair<int,pii> > v1;
set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    int i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(c,i),mp(a,b)));
        st.insert(b-a+1);
    }

    set<LL>:: iterator it;

    for(it=st.begin();it!=st.end();it++)
    {
        LL k=*it;
        sort(v[k].begin(),v[k].end());
    }
    LL mn=1e12;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size())
        {
            for(j=0;j<v[x-k].size();j++)
            {
                LL cst=v[x-k][j].first.first;
                LL pos=v[x-k][j].first.second;
                LL a1=v[x-k][j].second.first;
                LL b1=v[x-k][j].second.second;
                if(b1<a || b<a1)
                {
                    if(mn>cst+c && i!=pos)
                    {
                        mn=cst+c;
                        break;
                    }
                }
            }
        }
    }

if(mn==1e12)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<LL,LL>
using namespace std;

vector<pair<pii,pii> > v[200005];
vector<pair<int,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    int i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(c,i),mp(a,b)));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)

        sort(v[k].begin(),v[k].end());

    LL mn=1e12;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size())
        {
            for(j=0;j<v[x-k].size();j++)
            {
                LL cst=v[x-k][j].first.first;
                LL pos=v[x-k][j].first.second;
                LL a1=v[x-k][j].second.first;
                LL b1=v[x-k][j].second.second;
                if(b1<a || b<a1)
                {
                    if(mn>cst+c && i!=pos)
                    {
                        mn=cst+c;
                        break;
                    }
                }
            }
        }
    }

if(mn==1e12)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL long long int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<LL,LL>
using namespace std;

vector< pair<pii,int> > v[200005];
vector<pair<int,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    int i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(a,c),i));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)

        sort(v[k].begin(),v[k].end());

        for(i=1;i<=200000;i++)
        {
            if(v[i].size())
            {
            for(j=v[i].size()-2;j>=0;j--)
            {
                v[i][j].first.second=min(v[i][j].first.second,v[i][j+1].first .second);
            }
            }
        }

    LL mn=INT_MAX;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size())
        {
           int pos=upper_bound(v[x-k].begin(),v[x-k].end(),mp(mp(b,-1),-1))-v[x-k].begin();
           if(pos!=v[x-k].size() && v[x-k][pos].second!=i)
           {
               mn=min(mn,c+v[x-k][pos].first.second);
           }
        }
    }

if(mn==INT_MAX)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<int,int>
using namespace std;

vector< pair<pii,LL> > v[200005];
vector<pair<LL,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    LL i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(a,c),i));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)
         if(v[k].size())
        sort(v[k].begin(),v[k].end());

        for(i=1;i<=200000;i++)
        {
            if(v[i].size())
            {
            for(j=v[i].size()-2;j>=0;j--)
            {
                v[i][j].first.second=min(v[i][j].first.second,v[i][j+1].first .second);
            }
            }
        }

    LL mn=INT_MAX;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size()>0)
        { LL p=0;
           LL pos=upper_bound(v[x-k].begin(),v[x-k].end(),mp(mp(b,-1),-1))-v[x-k].begin();
           if(pos<v[x-k].size() && v[x-k][pos].second!=i)
           {
               mn=min(mn,c+v[x-k][pos].first.second);
           }
        }
    }

if(mn==INT_MAX)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<LL,LL>
using namespace std;

vector< pair<pii,int> > v[200005];
vector<pair<int,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    int i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(a,c),i));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)

        sort(v[k].begin(),v[k].end());

        for(i=1;i<=200000;i++)
        {
            if(v[i].size())
            {
            for(j=v[i].size()-2;j>=0;j--)
            {
                v[i][j].first.second=min(v[i][j].first.second,v[i][j+1].first .second);
            }
            }
        }

    LL mn=INT_MAX;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size())
        {
           int pos=upper_bound(v[x-k].begin(),v[x-k].end(),mp(mp(b,-1),-1))-v[x-k].begin();
           if(pos!=v[x-k].size() && v[x-k][pos].second!=i)
           {
               mn=min(mn,c+v[x-k][pos].first.second);
           }
        }
    }

if(mn==INT_MAX)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL long long int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<LL,LL>
using namespace std;

vector<pair<pii,pii> > v[200005];
vector<pair<int,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    int i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(c,i),mp(a,b)));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)

        sort(v[k].begin(),v[k].end());

    LL mn=1e12;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size())
        {
            for(j=0;j<v[x-k].size();j++)
            {
                LL cst=v[x-k][j].first.first;
                LL pos=v[x-k][j].first.second;
                LL a1=v[x-k][j].second.first;
                LL b1=v[x-k][j].second.second;
                if(b1<a || b<a1)
                {
                    if(mn>cst+c && i!=pos)
                    {
                        mn=cst+c;
                        break;
                    }
                }
            }
        }
    }

if(mn==1e12)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include<bits/stdc++.h>
#define LL int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<LL,LL>
using namespace std;

vector< pii > v[200005];
vector<pair<int,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    int i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(a,c));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)

        sort(v[k].begin(),v[k].end());

        for(i=1;i<=200000;i++)
        {
            if(v[i].size())
            {
            for(j=v[i].size()-2;j>=0;j--)
            {
                v[i][j].second=min(v[i][j].second,v[i][j+1].second);
            }
            }
        }

    LL mn=INT_MAX;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size())
        {
           int pos=upper_bound(v[x-k].begin(),v[x-k].end(),mp(b,0))-v[x-k].begin();
           if(pos!=v[x-k].size())
           {
               mn=min(mn,c+v[x-k][pos].second);
           }
        }
    }

if(mn==INT_MAX)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAXN 200007
using namespace std;

typedef long long ll;

struct Voc
{
	ll l, r, c, val;
	void in()
	{
		cin >> l >> r >> c;
		val = r - l + 1;
	}
}voc[MAXN];

ll v [MAXN];

bool cmpl(int a, int b)
{
	return voc[a].l < voc[b].l;
}
bool cmpr(int a, int b)
{
	return voc[a].r < voc[b].r;
}
int a[MAXN], b[MAXN];
int main()
{
	ll n, x;
	while (cin >> n >> x)
	{
		for (int i = 0; i < n; i++)
		{
			voc[i].in();
		}
		for (int i = 0; i < n; i++)
		{
			a[i] = i;
			b[i] = i;
		}
		sort(a, a+n, cmpl);
		sort(b, b+n, cmpr);
		int  p = 0;
		//memset(v, 0, sizeof(v));
		ll mincost = INF;
		for (int i = 0; i < MAXN; i++) v[i] = INF;
		//cout << mincost << endl;
		for (int i = 0; i < n; i++)
		{
			while (p < n && voc[b[p]].r < voc[a[i]].l)
			{
				v[voc[b[p]].val] = min(v[voc[b[p]].val], voc[b[p]].c);
				p++;
			}
			ll delta = x - voc[a[i]].val;
			if (delta > 0 && v[delta] != INF) mincost = min(mincost, voc[a[i]].c + v[delta]);
		}
		if (mincost == INF) cout << -1 << endl;
		else cout << mincost << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define coutF cout<<setiosflags(ios::fixed)<<setprecision(4)
#define iosfast std::ios::sync_with_stdio(false)
#define clr(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn=2e5+7;
const ll INF=4557430888798830399;
int n,m,x;
struct node{
	ll l,r,c,val;
	void in(){
		cin>>l>>r>>c;
		val=r-l+1;
	}
	void out(){
		cout<<l<<" "<<r<<" "<<c<<endl;
	}
}N[maxn];

bool cmp1(int a,int b){
	return N[a].l<N[b].l;
}
bool cmp2(int a,int b){
	return N[a].r<N[b].r;
}
int a[maxn],b[maxn];
ll v[maxn];
int main(){
	#ifndef ONLINE_JUDGE
	freopen("data.txt","r",stdin);
//	freopen("out.txt","w",stdout);
	#endif
	iosfast;

	while(cin>>n>>x){
		for(int i=0;i<n;i++)N[i].in(),a[i]=b[i]=i;
		sort(a,a+n,cmp1);
		sort(b,b+n,cmp2);
		clr(v,63);
		int p=0;
		ll mincost=INF;
		for(int i=0;i<n;i++){
			while(p<n&&N[b[p]].r<N[a[i]].l)v[N[b[p]].val]=min(v[N[b[p]].val],N[b[p]].c),p++;
			int fu=x-N[a[i]].val;
			if(fu>0&&v[fu]!=INF)mincost=min(mincost,v[fu]+N[a[i]].c);
		}
		if(mincost==INF)cout<<-1<<endl;
		else cout<<mincost<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;

typedef long long ll;

struct Voc
{
	int l, r, val;
	ll cost;
	void in()
	{
		cin >> l >> r >> cost;
		val = r - l + 1;
	}
}voc[200007];
int a[200007], b[200007];
int n, x;
bool cmpl(int x, int y)
{
	return voc[x].l < voc[y].l;
}
bool cmpr(int x, int y)
{
	return voc[x].r < voc[y].r;
}
ll cost[200007];
int main()
{
	while (cin >> n >> x)
	{
		for (int i = 0; i < n; i++)
		{
			voc[i].in();
			a[i] = i;
			b[i] = i;
		}
		sort(a, a+n, cmpl);
		sort(b, b+n, cmpr);
		ll mincost = INF;
		memset(cost, 0x3f, sizeof(cost));
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (voc[j].r < voc[i].l) cost[voc[j].val] = min(cost[voc[j].val], voc[j].cost);
				else break;
			}
			int res = x - voc[i].val;
			if (res > 0 && cost[res] < INF)
				mincost = min(mincost, cost[res]+voc[i].cost);
		}
		if (mincost >= INF) cout << -1 << endl;
		else cout << mincost << endl;
	}

}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
#define MAXN 200007
using namespace std;

typedef long long ll;

struct Voc
{
	ll l, r, c, val;
	void in()
	{
		cin >> l >> r >> c;
		val = r - l + 1;
	}
}voc[MAXN];

bool cmpl(int a, int b)
{
	return voc[a].l < voc[b].l;
}
bool cmpr(int a, int b)
{
	return voc[a].r < voc[b].r;
}
int a[MAXN], b[MAXN];
ll v [MAXN];
int main()
{
	int n, x;

	while (cin >> n >> x)
	{
		for (int i = 0; i < n; i++)
		{
			voc[i].in();
		}
		for (int i = 0; i < n; i++)
		{
			a[i] = i;
			b[i] = i;
		}
		sort(a, a+n, cmpl);
		sort(b, b+n, cmpr);
		int  p = 0;
		//memset(v, 63, sizeof(v));
		ll mincost = INF;
		//cout << v[0] << endl;
		for (int i = 0; i < MAXN; i++) v[i] = INF;
		//cout << mincost << endl;
		for (int i = 0; i < n; i++)
		{
			while (p < n && voc[b[p]].r < voc[a[i]].l)
			{
				v[voc[b[p]].val] = min(v[voc[b[p]].val], voc[b[p]].c);
				p++;
			}
			int delta = x - voc[a[i]].val;
			if (delta > 0 && v[delta] != INF) mincost = min(mincost, voc[a[i]].c + v[delta]);
		}
		if (mincost == INF) cout << -1 << endl;
		else cout << mincost << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAXN 200007
using namespace std;

typedef long long ll;

struct Voc
{
	ll l, r, c, val;
	void in()
	{
		cin >> l >> r >> c;
		val = r - l + 1;
	}
}voc[MAXN];

ll v [MAXN];

bool cmpl(int a, int b)
{
	return voc[a].l < voc[b].l;
}
bool cmpr(int a, int b)
{
	return voc[a].r < voc[b].r;
}
int a[MAXN], b[MAXN];
int main()
{
	ll n, x;
	while (cin >> n >> x)
	{
		for (int i = 0; i < n; i++)
		{
			voc[i].in();
		}
		for (int i = 0; i < n; i++)
		{
			a[i] = i;
			b[i] = i;
		}
		sort(a, a+n, cmpl);
		sort(b, b+n, cmpr);
		int  p = 0;
		//memset(v, 0, sizeof(v));
		ll mincost = INF;
		for (int i = 0; i < MAXN; i++) v[i] = INF;
		//cout << mincost << endl;
		for (int i = 0; i < n; i++)
		{
			while (p < n && voc[b[p]].r < voc[a[i]].l)
			{
				v[voc[b[p]].val] = min(v[voc[b[p]].val], voc[b[p]].c);
				p++;
			}
			ll delta = x - voc[a[i]].val;
			if (delta > 0 && v[delta] != INF) mincost = min(mincost, voc[a[i]].c + v[delta]);
		}
		if (mincost < INF) cout << mincost << endl;
		else cout << -1 << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAXN 200007
using namespace std;

typedef long long ll;

struct Voc
{
	ll l, r, c, val;
	void in()
	{
		cin >> l >> r >> c;
		val = r - l + 1;
	}
}voc[MAXN];

ll v [MAXN];

bool cmpl(int a, int b)
{
	return voc[a].l < voc[b].l;
}
bool cmpr(int a, int b)
{
	return voc[a].r < voc[b].r;
}
int a[MAXN], b[MAXN];
int main()
{
	ll n, x;
	while (cin >> n >> x)
	{
		for (int i = 0; i < n; i++)
		{
			voc[i].in();
		}
		for (int i = 0; i < n; i++)
		{
			a[i] = i;
			b[i] = i;
		}
		sort(a, a+n, cmpl);
		sort(b, b+n, cmpr);
		int  p = 0;
		memset(v, 63, sizeof(v));
		ll mincost = INF;
		//cout << v[0] << endl;
		//for (int i = 0; i < MAXN; i++) v[i] = INF;
		//cout << mincost << endl;
		for (int i = 0; i < n; i++)
		{
			while (p < n && voc[b[p]].r < voc[a[i]].l)
			{
				v[voc[b[p]].val] = min(v[voc[b[p]].val], voc[b[p]].c);
				p++;
			}
			int delta = x - voc[a[i]].val;
			if (delta > 0 && v[delta] != INF) mincost = min(mincost, voc[a[i]].c + v[delta]);
		}
		if (mincost == INF) cout << -1 << endl;
		else cout << mincost << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;

typedef long long ll;

struct Voc
{
	int l, r, val;
	ll cost;
	void in()
	{
		//cin >> l >> r >> cost;
		scanf("%d%d%d", &l, &r, &cost);
		val = r - l + 1;
	}
}voc[200007];
int a[200007], b[200007];
int n, x;
bool cmpl(int x, int y)
{
	return voc[x].l < voc[y].l;
}
bool cmpr(int x, int y)
{
	return voc[x].r < voc[y].r;
}
ll cost[200007];
int main()
{
	while (cin >> n >> x)
	{
		for (int i = 0; i < n; i++)
		{
			voc[i].in();
			a[i] = i;
			b[i] = i;
		}
		sort(a, a+n, cmpl);
		sort(b, b+n, cmpr);
		ll mincost = INF;
		memset(cost, 0x3f, sizeof(cost));
		int p = 0;
		for (int i = 0; i < n; i++)
		{
			while (p < n && voc[b[p]].r < voc[a[i]].l ) //这里不需要每次都从0开始 只需要更新新添加的右区间即可
			{
				if (voc[b[p]].r < voc[a[i]].l) cost[voc[b[p]].val] = min(cost[voc[b[p]].val], voc[b[p]].cost);
				p++;
			}
			int res = x - voc[a[i]].val;
			if (res > 0 && cost[res] < INF)
				mincost = min(mincost, cost[res]+voc[a[i]].cost);
		}
		if (mincost >= INF) cout << -1 << endl;
		else cout << mincost << endl;
	}
	return 0;

}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAXN 200007
using namespace std;

typedef long long ll;

struct Voc
{
	ll l, r, c, val;
	void in()
	{
		cin >> l >> r >> c;
		val = r - l + 1;
	}
}voc[MAXN];

ll v [MAXN];

bool cmpl(int a, int b)
{
	return voc[a].l < voc[b].l;
}
bool cmpr(int a, int b)
{
	return voc[a].r < voc[b].r;
}
int a[MAXN], b[MAXN];
int main()
{
	int n, x;
	while (cin >> n >> x)
	{
		for (int i = 0; i < n; i++)
		{
			voc[i].in();
		}
		for (int i = 0; i < n; i++)
		{
			a[i] = i;
			b[i] = i;
		}
		sort(a, a+n, cmpl);
		sort(b, b+n, cmpr);
		int  p = 0;
		memset(v, 63, sizeof(v));
		ll mincost = INF;
		//cout << v[0] << endl;
		//for (int i = 0; i < MAXN; i++) v[i] = INF;
		//cout << mincost << endl;
		for (int i = 0; i < n; i++)
		{
			while (p < n && voc[b[p]].r < voc[a[i]].l)
			{
				v[voc[b[p]].val] = min(v[voc[b[p]].val], voc[b[p]].c);
				p++;
			}
			int delta = x - voc[a[i]].val;
			if (delta > 0 && v[delta] != INF) mincost = min(mincost, voc[a[i]].c + v[delta]);
		}
		if (mincost == INF) cout << -1 << endl;
		else cout << mincost << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x;
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        int ans=-1;
        int cnt,l,r,mid;
        for(int i=0;i<n;i++)
        {
            if(e[i].num>=x)
                break;
            cnt=x-e[i].num;
            l=0;
            r=n-1;
            while(l<=r)
            {
                mid=l+r>>1;
                if(e[mid].num<cnt)
                    l=mid+1;
                else
                    r=mid-1;
            }
            for(int j=l;e[j].num==cnt;j++)
            {
                if(j==i)
                    continue;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,m,mini[maxn],lid[maxn],rid[maxn];
struct node
{
    int l;
    int r;
    int num;
    int c;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.l<b.l;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        mini[n-1]=e[n-1].c;
        for(int i=n-2;i>=0;i--)
        {
            if(e[i].num==e[i+1].num)
                mini[i]=min(mini[i+1],e[i].c);
            else
                mini[i]=e[i].c;
        }
        memset(lid,-1,sizeof(lid));
        memset(rid,-1,sizeof(rid));
        for(int i=0;i<n;i++)
        {
            int x=e[i].num;
            if(lid[x]==-1)
            {
                lid[x]=i;
                rid[x]=i;
            }
            else
                rid[x]=i;
        }
        int L,R,mid;
        int ans=-1;
        for(int i=0;i<n;i++)
        {
            int x=m-e[i].num;
            if(lid[x]==-1)
                continue;
            L=lid[x];
            R=rid[x];
            while(L<=R)
            {
                mid=L+R>>1;
                if(e[mid].l<=e[i].r)
                    L=mid+1;
                else
                    R=mid-1;
            }
            //printf("sss      %d    %d\n",cnt,e[cnt].l);
            if(L>rid[x])
                continue;
            if(ans==-1)
                ans=e[i].c+mini[L];
            else
                ans=min(ans,e[i].c+mini[L]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x;
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        int ans=-1;
        int cnt,l,r,mid;
        for(int i=0;i<n;i++)
        {
            if(e[i].num>=x)
                break;
            cnt=x-e[i].num;
            l=0;
            r=n-1;
            while(l<=r)
            {
                mid=l+r>>1;
                if(e[mid].num<cnt)
                    l=mid+1;
                else
                    r=mid-1;
            }
            for(int j=l;e[j].num==cnt;j++)
            {
                if(j==i)
                    continue;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x,lid[maxn],rid[maxn];
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        memset(lid,-1,sizeof(lid));
        memset(rid,-1,sizeof(rid));
        for(int i=0;i<n;i++)
        {
            int temp=e[i].num;
            if(lid[temp]==-1)
            {
                lid[temp]=i;
                rid[temp]=i;
            }
            else
                rid[temp]=i;
        }
        int ans=-1;
        for(int i=0;i<n;i++)
        {
            int temp=x-e[i].num;
            if(lid[temp]==-1)
                continue;
            for(int j=lid[temp];j<=rid[temp];j++)
            {
                if(i==j)
                    continue;
                if(e[i].c+e[j].c>=ans&&ans!=-1)
                    break;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
const int inf=1<<30;
int n,m,mini[maxn];
struct node
{
    int l;
    int r;
    int num;
    int c;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.l<b.l;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        mini[n-1]=e[n-1].c;
        for(int i=n-2;i>=0;i--)
        {
            mini[i]=inf;
            if(e[i].num==e[i+1].num)
                mini[i]=min(mini[i+1],e[i].c);
            else
                mini[i]=e[i].c;
        }
        int L,R,mid;
        int ans=-1;
        for(int i=0;i<n;i++)
        {
            int x=m-e[i].num;
            L=0;
            R=n-1;
            int cnt=-1;
            while(L<=R)
            {
                mid=L+R>>1;
                if(e[mid].num<x)
                    L=mid+1;
                else if(e[mid].num==x)
                {
                    if(e[mid].l<=e[i].r)
                    {
                        cnt=mid+1;
                        L=mid+1;
                    }
                    else
                        R=mid-1;
                }
                else
                    R=mid-1;
            }
            //printf("sss      %d    %d\n",cnt,e[cnt].l);
            if(cnt==-1||cnt>=n)
                continue;
            if(ans==-1)
                ans=e[i].c+mini[cnt];
            else
                ans=min(ans,e[i].c+mini[cnt]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x;
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        int ans=-1;
        int cnt,l,r,mid;
        for(int i=0;i<n;i++)
        {
            if(e[i].num>=x)
                break;
            if(e[i].c>=ans&&ans!=-1)
                continue;
            cnt=x-e[i].num;
            l=0;
            r=n-1;
            while(l<=r)
            {
                mid=l+r>>1;
                if(e[mid].num<cnt)
                    l=mid+1;
                else
                    r=mid-1;
            }
            for(int j=l;e[j].num==cnt;j++)
            {
                if(j==i)
                    continue;
                if(e[i].c+e[j].c>=ans)
                    break;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
const int inf=1<<30;
int n,m,mini[maxn],lid[maxn],rid[maxn];
struct node
{
    int l;
    int r;
    int num;
    int c;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.l<b.l;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        mini[n-1]=e[n-1].c;
        for(int i=n-2;i>=0;i--)
        {
            mini[i]=inf;
            if(e[i].num==e[i+1].num)
                mini[i]=min(mini[i+1],e[i].c);
            else
                mini[i]=e[i].c;
        }
        memset(lid,-1,sizeof(lid));
        memset(rid,-1,sizeof(rid));
        for(int i=0;i<n;i++)
        {
            int x=e[i].num;
            if(lid[x]==-1)
            {
                lid[x]=i;
                rid[x]=i;
            }
            else
                rid[x]=i;
        }
        int L,R,mid;
        int ans=-1;
        for(int i=0;i<n;i++)
        {
            int x=m-e[i].num;
            if(lid[x]==-1)
                continue;
            L=lid[x];
            R=rid[x];
            while(L<=R)
            {
                mid=L+R>>1;
                if(e[mid].l<=e[i].r)
                    L=mid+1;
                else
                    R=mid-1;
            }
            //printf("sss      %d    %d\n",cnt,e[cnt].l);
            if(L>rid[x])
                continue;
            if(ans==-1)
                ans=e[i].c+mini[L];
            else
                ans=min(ans,e[i].c+mini[L]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x,lid[maxn],rid[maxn];
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        memset(lid,-1,sizeof(lid));
        memset(rid,-1,sizeof(rid));
        for(int i=0;i<n;i++)
        {
            int temp=e[i].num;
            if(lid[temp]==-1)
            {
                lid[temp]=i;
                rid[temp]=i;
            }
            else
                rid[temp]=i;
        }
        int ans=-1;
        for(int i=0;i<n;i++)
        {
            if(e[i].c>=ans&&ans!=-1)
                break;
            if(e[i].num>=x)
                break;
            int temp=x-e[i].num;
            if(lid[temp]==-1)
                continue;
            for(int j=lid[temp];j<=rid[temp];j++)
            {
                if(i==j)
                    continue;
                if(e[i].c+e[j].c>=ans&&ans!=-1)
                    break;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,m,mini[maxn],lid[maxn],rid[maxn];
struct node
{
    int l;
    int r;
    int num;
    int c;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.l<b.l;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        mini[n-1]=e[n-1].c;
        for(int i=n-2;i>=0;i--)
        {
            if(e[i].num==e[i+1].num)
                mini[i]=min(mini[i+1],e[i].c);
            else
                mini[i]=e[i].c;
        }
        memset(lid,-1,sizeof(lid));
        memset(rid,-1,sizeof(rid));
        for(int i=0;i<n;i++)
        {
            int x=e[i].num;
            if(lid[x]==-1)
            {
                lid[x]=i;
                rid[x]=i;
            }
            else
                rid[x]=i;
        }
        int L,R,mid;
        int ans=-1;
        for(int i=0;i<n;i++)
        {
            if(e[i].num>=m)
                break;
            int x=m-e[i].num;
            if(lid[x]==-1)
                continue;
            L=lid[x];
            R=rid[x];
            while(L<=R)
            {
                mid=L+R>>1;
                if(e[mid].l<=e[i].r)
                    L=mid+1;
                else
                    R=mid-1;
            }
            //printf("sss      %d    %d\n",cnt,e[cnt].l);
            if(L>rid[x])
                continue;
            if(ans==-1)
                ans=e[i].c+mini[L];
            else
                ans=min(ans,e[i].c+mini[L]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x;
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        int ans=-1;
        int cnt,l,r,mid;
        for(int i=0;i<n;i++)
        {
            if(e[i].l==e[i-1].l&&e[i].num==e[i-1].num)
                continue;
            if(e[i].num>=x)
                break;
            if(e[i].c>=ans&&ans!=-1)
                continue;
            cnt=x-e[i].num;
            l=0;
            r=n-1;
            while(l<=r)
            {
                mid=l+r>>1;
                if(e[mid].num<cnt)
                    l=mid+1;
                else
                    r=mid-1;
            }
            for(int j=l;e[j].num==cnt;j++)
            {
                if(j==i)
                    continue;
                if(e[i].c+e[j].c>=ans&&ans!=-1)
                    break;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x,lid[maxn],rid[maxn];
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        memset(lid,-1,sizeof(lid));
        memset(rid,-1,sizeof(rid));
        for(int i=0;i<n;i++)
        {
            int temp=e[i].num;
            if(lid[temp]==-1)
            {
                lid[temp]=i;
                rid[temp]=i;
            }
            else
                rid[temp]=i;
        }
        int ans=-1;
        for(int i=0;i<n;i++)
        {
            int temp=x-e[i].num;
            for(int j=lid[temp];j<=rid[temp];j++)
            {
                if(i==j)
                    continue;
                if(e[i].c+e[j].c>=ans&&ans!=-1)
                    break;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x,lid[maxn],rid[maxn];
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        memset(lid,-1,sizeof(lid));
        memset(rid,-1,sizeof(rid));
        for(int i=0;i<n;i++)
        {
            int temp=e[i].num;
            if(lid[temp]==-1)
            {
                lid[temp]=i;
                rid[temp]=i;
            }
            else
                rid[temp]=i;
        }
        int ans=-1;
        for(int i=0;i<n;i++)
        {
            if(e[i].c>=ans&&ans!=-1)
                continue;
            if(e[i].num>=x)
                break;
            int temp=x-e[i].num;
            if(lid[temp]==-1)
                continue;
            for(int j=lid[temp];j<=rid[temp];j++)
            {
                if(i==j)
                    continue;
                if(e[i].c+e[j].c>=ans&&ans!=-1)
                    break;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x;
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        int ans=-1;
        int cnt,l,r,mid;
        for(int i=0;i<n;i++)
        {
            if(e[i].num>=x)
                break;
            if(e[i].c>=ans&&ans!=-1)
                continue;
            cnt=x-e[i].num;
            l=0;
            r=n-1;
            while(l<=r)
            {
                mid=l+r>>1;
                if(e[mid].num<cnt)
                    l=mid+1;
                else
                    r=mid-1;
            }
            for(int j=l;e[j].num==cnt;j++)
            {
                if(j==i)
                    continue;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n,x;
struct node
{
    int l;
    int r;
    int c;
    int num;
}e[maxn];
bool cmp(node a,node b)
{
    if(a.num==b.num)
        return a.c<b.c;
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&e[i].l,&e[i].r,&e[i].c);
            e[i].num=e[i].r-e[i].l+1;
        }
        sort(e,e+n,cmp);
        int ans=-1;
        int cnt,l,r,mid;
        for(int i=0;i<n;i++)
        {
            if(e[i].num>=x)
                break;
            if(e[i].c>=ans&&ans!=-1)
                continue;
            cnt=x-e[i].num;
            l=0;
            r=n-1;
            while(l<=r)
            {
                mid=l+r>>1;
                if(e[mid].num<cnt)
                    l=mid+1;
                else
                    r=mid-1;
            }
            for(int j=l;e[j].num==cnt;j++)
            {
                if(j==i)
                    continue;
                if(e[i].c+e[j].c>=ans&&ans!=-1)
                    break;
                if(e[i].l>e[j].r||e[i].r<e[j].l)
                {
                    if(ans==-1)
                        ans=e[i].c+e[j].c;
                    else
                        ans=min(ans,e[i].c+e[j].c);
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

struct Voucher {
    int l, r, c;
    Voucher() {}
    Voucher(int a, int b) {
        l = a;
        r = b;
        c = 0;
    }
};

bool cmp(const Voucher &a, const Voucher &b) {
    return a.r - a.l == b.r - b.l ? a.l < b.l : a.r - a.l < b.r - b.l;
}

const int N = 200010;
const int ST_SIZE = 20;
Voucher v[N];
int st[N][ST_SIZE];

//st数组格式st[MAXN][ST_SIZE]
//a数组从1开始计数
void Init(Voucher *a, int n) {
    for (int i = 0; i < n; i++)
        st[i][0] = a[i].c;
    for (int j = 1; j < ST_SIZE; j++) {
        for (int i = 1; i + (1 << j) <= n; i++)
            st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
    }
}

int Query(int l, int r) {
    int k = (int)log2(r + 1 - l + 0.000001);
    return min(st[l][k], st[r + 1 - (1 << k)][k]);
}

int main() {
    int n, x; scanf("%d%d", &n, &x);
    for (int i = 0; i < n; i++) scanf("%d%d%d", &v[i].l, &v[i].r, &v[i].c);
    sort(v, v + n, cmp);
    Init(v, n);
    int ans = 2000000001;
    for (int i = 0; i < n; i++) {
        int t = x - (v[i].r - v[i].l + 1);
        if (t <= 0) continue;
        int p1 = upper_bound(v, v + n, Voucher(v[i].r, v[i].r + t - 1), cmp) - v;
        int p2 = lower_bound(v, v + n, Voucher(0, t), cmp) - v - 1;
        if (p1 >= n || p2 >= n || p1 > p2) continue;
        ans = min(ans, Query(p1, p2) + v[i].c);
    }
    printf("%d\n", ans == 2000000001 ? -1 : ans);
    return 0;
}


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

struct Voucher {
    int l, r, c;
    Voucher() {}
    Voucher(int a) {
        l = a;
    }
};

bool cmp(const Voucher &a, const Voucher &b) {
    return a.l < b.l;
}

const int N = 200010;
Voucher v[N];
vector<Voucher> g[N];

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

    int ans = 0x3f3f3f3f;
    for (int i = 1; i <= n; i++) {
        int t = x - (v[i].r - v[i].l + 1);
        vector<Voucher>::iterator it = upper_bound(g[t].begin(), g[t].end(), Voucher(v[i].r), cmp);
        for (; it != g[t].end(); it++) ans = min(ans, v[i].c + it->c);
    }
    printf("%d\n", ans == 0x3f3f3f3f ? -1 : ans);
    return 0;
}


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

struct Voucher {
    int l, r, c;
    Voucher() {}
    Voucher(int a) {
        l = a;
        r = c = 0;
    }
};

bool cmp(const Voucher &a, const Voucher &b) {
    return a.l < b.l;
}

const int N = 200010;
Voucher v[N];
vector<Voucher> g[N];

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

    int ans = 2000000001;
    for (int i = 1; i <= n; i++) {
        int t = x - (v[i].r - v[i].l + 1);
        if (t <= 0) continue;
        vector<Voucher>::iterator it = upper_bound(g[t].begin(), g[t].end(), Voucher(v[i].r), cmp);
        for (; it != g[t].end(); it++) ans = min(ans, v[i].c + it->c);
    }
    printf("%d\n", ans == 2000000001 ? -1 : ans);
    return 0;
}


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

struct Voucher {
    int l, r, c;
    Voucher() {}
    Voucher(int a) {
        l = a;
        r = c = 0;
    }
};

bool cmp(const Voucher &a, const Voucher &b) {
    return a.r - a.l < b.r - b.l;
}

const int N = 200010;
Voucher v[N];

int main() {
    int n, x; scanf("%d%d", &n, &x);
    for (int i = 0; i < n; i++) scanf("%d%d%d", &v[i].l, &v[i].r, &v[i].c);
    sort(v, v + n, cmp);
    int ans = 2000000001;
    for (int i = 0; i < n; i++) {
        int t = v[i].r - v[i].l + 1;
        if (t <= 0) continue;
        int p = lower_bound(v + i + 1, v + n, v[i], cmp) - v;
        for (; p < n; p++) {
            if (v[p].r - v[p].l > v[i].r - v[i].l) break;
            if (v[i].r < v[p].l || v[p].r < v[i].l) {
                ans = min(ans, v[i].c + v[p].c);
            }
        }
    }
    printf("%d\n", ans == 2000000001 ? -1 : ans);
    return 0;
}


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

struct Voucher {
    int l, r, c;
    Voucher() {}
    Voucher(int a) {
        l = a;
        r = c = 0;
    }
};

bool cmp(const Voucher &a, const Voucher &b) {
    return a.l < b.l;
}

const int N = 500010;
Voucher v[N];
vector<Voucher> g[N];

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

    int ans = 2000000001;
    for (int i = 1; i <= n; i++) {
        int t = x - (v[i].r - v[i].l + 1);
        vector<Voucher>::iterator it = upper_bound(g[t].begin(), g[t].end(), Voucher(v[i].r), cmp);
        for (; it != g[t].end(); it++) ans = min(ans, v[i].c + it->c);
    }
    printf("%d\n", ans == 2000000001 ? -1 : ans);
    return 0;
}


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

struct Voucher {
    int l, r, c;
    Voucher() {}
    Voucher(int a, int b) {
        l = a;
        r = b;
        c = 0;
    }
};

bool cmp(const Voucher &a, const Voucher &b) {
    return a.r - a.l < b.r - b.l;
}

const int N = 200010;
Voucher v[N];

int main() {
    int n, x; scanf("%d%d", &n, &x);
    for (int i = 0; i < n; i++) scanf("%d%d%d", &v[i].l, &v[i].r, &v[i].c);
    sort(v, v + n, cmp);
    int ans = 2000000001;
    for (int i = 0; i < n; i++) {
        int t = x - (v[i].r - v[i].l + 1);
        if (t <= 0) continue;
        int p = lower_bound(v + i + 1, v + n, Voucher(1, t), cmp) - v;
        for (; p < n; p++) {
            if (v[p].r - v[p].l + 1 + v[i].r - v[i].l + 1 != x) break;
            if (v[i].r < v[p].l || v[p].r < v[i].l) {
                ans = min(ans, v[i].c + v[p].c);
            }
        }
    }
    printf("%d\n", ans == 2000000001 ? -1 : ans);
    return 0;
}


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

struct Voucher {
    int l, r, c;
    Voucher() {}
    Voucher(int a, int b) {
        l = a;
        r = b;
        c = 0;
    }
};

bool cmp(const Voucher &a, const Voucher &b) {
    return a.r - a.l == b.r - b.l ? a.l < b.l : a.r - a.l < b.r - b.l;
}

const int N = 200010;
const int ST_SIZE = 20;
Voucher v[N];
int st[N][ST_SIZE];

//st数组格式st[MAXN][ST_SIZE]
//a数组从1开始计数
void Init(Voucher *a, int n) {
    for (int i = 0; i < n; i++)
        st[i][0] = a[i].c;
    for (int j = 1; j < ST_SIZE; j++) {
        for (int i = 0; i + (1 << j) <= n; i++)
            st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
    }
}

int Query(int l, int r) {
    int k = (int)log2(r + 1 - l + 0.000001);
    return min(st[l][k], st[r + 1 - (1 << k)][k]);
}

int main() {
    int n, x; scanf("%d%d", &n, &x);
    for (int i = 0; i < n; i++) scanf("%d%d%d", &v[i].l, &v[i].r, &v[i].c);
    sort(v, v + n, cmp);
    Init(v, n);
    int ans = 2000000001;
    for (int i = 0; i < n; i++) {
        int t = x - (v[i].r - v[i].l + 1);
        if (t <= 0) continue;
        int p1 = upper_bound(v, v + n, Voucher(v[i].r, v[i].r + t - 1), cmp) - v;
        int p2 = lower_bound(v, v + n, Voucher(0, t), cmp) - v - 1;
        if (p1 >= n || p2 >= n || p1 > p2) continue;
        //printf("i=%d, p1=%d, p2=%d, c1=%d, c2=%d\n", i, p1, p2, v[i].c, Query(p1, p2));
        ans = min(ans, Query(p1, p2) + v[i].c);
    }
    printf("%d\n", ans == 2000000001 ? -1 : ans);
    return 0;
}


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

struct Voucher {
    int l, r, c;
    Voucher() {}
    Voucher(int a, int b) {
        l = a;
        r = b;
        c = 0;
    }
};

bool cmp(const Voucher &a, const Voucher &b) {
    return a.r - a.l == b.r - b.l ? a.l < b.l : a.r - a.l < b.r - b.l;
}

const int N = 200010;
Voucher v[N];

int main() {
    int n, x; scanf("%d%d", &n, &x);
    for (int i = 0; i < n; i++) scanf("%d%d%d", &v[i].l, &v[i].r, &v[i].c);
    sort(v, v + n, cmp);
    int ans = 2000000001;
    for (int i = 0; i < n; i++) {
        int t = x - (v[i].r - v[i].l + 1);
        if (t <= 0) continue;
        //printf("%d %d\n", v[i].r, v[i].r + t - 1);
        int p = upper_bound(v, v + n, Voucher(v[i].r, v[i].r + t - 1), cmp) - v;
        //printf("p = %d\n", p);
        while (p < n && v[p].r - v[p].l + 1 == t) {
            ans = min(ans, v[p].c + v[i].c);
            p++;
        }
    }
    printf("%d\n", ans == 2000000001 ? -1 : ans);
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=1e9+10;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],cost[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int i=0;i<=N;++i)cost[i]=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && cost[m-v])
				ans=min(ans,cost[m-v]+w);
		}
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			cost[v]=min(w,cost[v]);
		}
	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=2147483647;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],A[maxn],B[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int i=0;i<=N;++i)A[i]=B[i]=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			B[v]=min(B[v],w);
		}
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && A[m-v]!=inf)
				ans=min(ans,A[m-v]+w);
			if(v<=m && B[m-v+1]!=inf)
				ans=min(ans,B[m-v+1]+w);
		}
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			A[v]=min(A[v],w); B[v]=inf;
		}
	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=2147483647;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],cost[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && cost[m-v])
				ans=min(ans,cost[m-v]+w);
		}
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			cost[v]=w;
		}
	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=2147483647;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],cost[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int i=0;i<=N;++i)cost[i]=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			cost[v]=min(cost[v],w);
			if(v==639 && m==640)printf("%d %d %d\n",v,w,cost[v]);
		}
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && cost[m-v]!=inf)
				ans=min(ans,cost[m-v]+w);
			if(ans!=inf && m==640)printf("%d %d %d\n",v,w,cost[m-v]);
		}
	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=2147483647;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],cost[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int i=0;i<=N;++i)cost[i]=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			cost[v]=min(cost[v],w);
		}
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && cost[m-v]!=inf)
				ans=min(ans,cost[m-v]+w);
			if(ans!=inf && m==640)printf("%d %d %d\n",v,w,cost[m-v]);
		}
	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=2147483647;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],cost[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int i=1;i<=n;++i)
		if(P[i].x==1)printf("%d %d %d\n",P[i].l,P[i].r,P[i].x);
	for(int i=1;i<=N;++i)cost[i]=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			cost[v]=min(cost[v],w);
		}
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && cost[m-v]!=inf)
				ans=min(ans,cost[m-v]+w);
		}

	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=2147483647;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],cost[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int i=1;i<=n;++i)
		if(P[i].x==1 && m==39)printf("%d %d %d\n",P[i].l,P[i].r,P[i].x);
	for(int i=1;i<=N;++i)cost[i]=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			cost[v]=min(cost[v],w);
		}
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && cost[m-v]!=inf)
				ans=min(ans,cost[m-v]+w);
		}

	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=2147483647;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],cost[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int i=0;i<=N;++i)cost[i]=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			cost[v]=min(cost[v],w);
			if(v==599 && m==640)printf("%d %d %d\n",v,w,cost[v]);
		}
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && cost[m-v]!=inf)
				ans=min(ans,cost[m-v]+w);
			if(ans!=inf && m==640)printf("%d %d %d\n",v,w,cost[m-v]);
		}
	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int inf=2147483647;
const int maxn=2e5+10;
struct data{int l,r,x;}P[maxn];
struct edge{int v,last,w;}L1[maxn],L2[maxn];
int n,m,N=2e5+1,P1,P2,H1[maxn],H2[maxn],cost[maxn];
void AD1(int a,int b,int c){L1[++P1]=(edge){b,H1[a],c}; H1[a]=P1;}
void AD2(int a,int b,int c){L2[++P2]=(edge){b,H2[a],c}; H2[a]=P2;}
void init()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
		scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].x);
	for(int i=1;i<=n;++i)AD1(P[i].l,P[i].r-P[i].l+1,P[i].x);
	for(int i=1;i<=n;++i)AD2(P[i].r,P[i].r-P[i].l+1,P[i].x);
}
void solve()
{
	int ans=inf;
	for(int i=0;i<=N;++i)cost[i]=inf;
	for(int l=1;l<=N;++l)
	{
		for(int i=H2[l];i;i=L2[i].last)
		{
			int v=L2[i].v,w=L2[i].w;
			cost[v]=min(cost[v],w);
//			if(v==639 && m==640)printf("%d %d %d %d\n",l,v,w,cost[v]);
		}
		for(int i=H1[l];i;i=L1[i].last)
		{
			int v=L1[i].v,w=L1[i].w;
			if(v<=m && cost[m-v]!=inf)
				ans=min(ans,cost[m-v]+w);
			if(ans!=inf && m==640)printf("%d %d %d %d\n",l,v,w,cost[m-v]);
		}
	}
	printf("%d\n",ans==inf ? -1 : ans);
}
int main()
{
	init();
	solve();
	return 0;
}


import java.util.*;
import java.io.*;
public class Task{
// taking inputs
static BufferedReader s;
static String read() throws IOException{String line="";while(line.length()==0){line=s.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
// ends
public static void main(String[] args) throws  IOException{
       s=new BufferedReader(new InputStreamReader(System.in));
       BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
        	String ss=read();
        	long[][] a=new long[26][26];
        	int[] cc=new int[26]; long k=0;
        	for(int i=0;i<ss.length();i++){
        		char c1=ss.charAt(i);
        		for(int j=0;j<26;j++){
        			a[j][c1-'a']+=cc[j];
        		}
        		cc[c1-'a']++;
        		k=Math.max(k,cc[c1-'a']);
        	}
        	for(int i=0;i<26;i++){
        		for(int j=0;j<26;j++){
        			k=Math.max(a[i][j],k);
        		}
        	}
        	out.write(k+"\n");
       out.flush();
   }
}


import java.util.*;
import java.io.*;
public class Task{
/// functions
static long gcd(long a,long b){if(b==0){return a;}return gcd(b,a%b);}
static long Modpow(long a,long p,long m){long res=1;while(p>0){if((p&1)!=0){res=(res*a)%m;}p >>=1;a=(a*a)%m;}return res;}
static long Modmul(long a,long b,long m){return ((a%m)*(b%m))%m;}
static long ModInv(long a,long m){return Modpow(a,m-2,m);}
static long nck(int n,int r,long m){if(r>n){return 0l;}return Modmul(f[n],ModInv(Modmul(f[n-r],f[r],m),m),m);}
static void mat_mul(long[][] a, long[][] b, long[][]tmp,long m){
	int n=a.length;
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
              long sum=0;
              for(int k=0;k<n;k++){
                    sum+=b[i][k]*a[k][j];
                    sum%=m;
               }
               tmp[i][j]=sum;
        }
     }
     for(int i=0;i<n;i++){a[i]=tmp[i].clone();}
}
static void fast_mat_exp(long[][] a,long m, long p){
      int n=a.length;
      long[][] res=new long[n][n];
      long[][] res1=new long[n][n];
      long[][] tmp=new long[n][n];
      for(int i=0;i<n;i++){res[i][i]=1;}// identity matrix;
      while(p>0){
            if(p%2==1){
                  mat_mul(res,a,res1,m); // multiply res*a. res1 storing res*a and again assigning it to res.
            }
            p/=2;
                 mat_mul(a,a,tmp,m);
      }
      //res contains result
 }
// end
static long[] f;

  static long phi(long n){
  	int k=(int)Math.sqrt(n);
  	long x=1,y=n;
  	for(int i=2;i<=k;i++){
  		boolean b=false;
  		while(n%i==0){
  			n/=i;b=true;
  		}
  		if(b){
  			 y/=i;
  		x*=(long)(i-1);
  		}
  	}
  	if(n>1){x*=(n-1);y/=n;}
  	return x*y;
  }
// taking inputs
static BufferedReader s;
static String read() throws IOException{String line="";while(line.length()==0){line=s.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
// ends
public static void main(String[] args) throws  IOException{
       s=new BufferedReader(new InputStreamReader(System.in));
       BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
       	StringBuilder sb=new StringBuilder();
        int t=1;
         while(t--!=0){
        	String ss=read();
        	int[][] a=new int[26][26];
        	Map<Character,Integer>map=new HashMap<>(); int k=0;
        	for(int i=0;i<ss.length();i++){
        		for(char c:map.keySet()){
        			a[c-'a'][ss.charAt(i)-'a']+=map.get(c);
        		}
        		map.put(ss.charAt(i),map.getOrDefault(ss.charAt(i),0)+1);
        		k=Math.max(k,map.get(ss.charAt(i)));
        	}
        	long res=0;
        	for(int i=0;i<26;i++){
        		for(int j=0;j<26;j++){
        			res=Math.max(a[i][j],res);
        		}
        	}
        	out.write(Math.max(k,res)+"\n");
       }
       out.flush();
   }
}


import java.util.*;
import java.io.*;
public class Task{
/// functions
static long gcd(long a,long b){if(b==0){return a;}return gcd(b,a%b);}
static long Modpow(long a,long p,long m){long res=1;while(p>0){if((p&1)!=0){res=(res*a)%m;}p >>=1;a=(a*a)%m;}return res;}
static long Modmul(long a,long b,long m){return ((a%m)*(b%m))%m;}
static long ModInv(long a,long m){return Modpow(a,m-2,m);}
static long nck(int n,int r,long m){if(r>n){return 0l;}return Modmul(f[n],ModInv(Modmul(f[n-r],f[r],m),m),m);}
static void mat_mul(long[][] a, long[][] b, long[][]tmp,long m){
	int n=a.length;
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
              long sum=0;
              for(int k=0;k<n;k++){
                    sum+=b[i][k]*a[k][j];
                    sum%=m;
               }
               tmp[i][j]=sum;
        }
     }
     for(int i=0;i<n;i++){a[i]=tmp[i].clone();}
}
static void fast_mat_exp(long[][] a,long m, long p){
      int n=a.length;
      long[][] res=new long[n][n];
      long[][] res1=new long[n][n];
      long[][] tmp=new long[n][n];
      for(int i=0;i<n;i++){res[i][i]=1;}// identity matrix;
      while(p>0){
            if(p%2==1){
                  mat_mul(res,a,res1,m); // multiply res*a. res1 storing res*a and again assigning it to res.
            }
            p/=2;
                 mat_mul(a,a,tmp,m);
      }
      //res contains result
 }
// end
static long[] f;

  static long phi(long n){
  	int k=(int)Math.sqrt(n);
  	long x=1,y=n;
  	for(int i=2;i<=k;i++){
  		boolean b=false;
  		while(n%i==0){
  			n/=i;b=true;
  		}
  		if(b){
  			 y/=i;
  		x*=(long)(i-1);
  		}
  	}
  	if(n>1){x*=(n-1);y/=n;}
  	return x*y;
  }
// taking inputs
static BufferedReader s;
static String read() throws IOException{String line="";while(line.length()==0){line=s.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
// ends
public static void main(String[] args) throws  IOException{
       s=new BufferedReader(new InputStreamReader(System.in));
       BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
       	StringBuilder sb=new StringBuilder();
        int t=1;
         while(t--!=0){
        	String ss=read();
        	int[][] a=new int[26][26];
        	Map<Character,Integer>map=new HashMap<>();
        	for(int i=0;i<ss.length();i++){
        		for(char c:map.keySet()){
        			a[c-'a'][ss.charAt(i)-'a']+=map.get(c);
        		}
        		map.put(ss.charAt(i),map.getOrDefault(ss.charAt(i),0)+1);
        	}
        	long res=0;
        	for(int i=0;i<26;i++){
        		for(int j=0;j<26;j++){
        			res=Math.max(a[i][j],res);
        		}
        	}
        	out.write(Math.max(1,res)+"\n");
       }
       out.flush();
   }
}


import java.util.*;
import java.io.*;
public class Task{
/// functions
static long gcd(long a,long b){if(b==0){return a;}return gcd(b,a%b);}
static long Modpow(long a,long p,long m){long res=1;while(p>0){if((p&1)!=0){res=(res*a)%m;}p >>=1;a=(a*a)%m;}return res;}
static long Modmul(long a,long b,long m){return ((a%m)*(b%m))%m;}
static long ModInv(long a,long m){return Modpow(a,m-2,m);}
static long nck(int n,int r,long m){if(r>n){return 0l;}return Modmul(f[n],ModInv(Modmul(f[n-r],f[r],m),m),m);}
static void mat_mul(long[][] a, long[][] b, long[][]tmp,long m){
	int n=a.length;
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
              long sum=0;
              for(int k=0;k<n;k++){
                    sum+=b[i][k]*a[k][j];
                    sum%=m;
               }
               tmp[i][j]=sum;
        }
     }
     for(int i=0;i<n;i++){a[i]=tmp[i].clone();}
}
static void fast_mat_exp(long[][] a,long m, long p){
      int n=a.length;
      long[][] res=new long[n][n];
      long[][] res1=new long[n][n];
      long[][] tmp=new long[n][n];
      for(int i=0;i<n;i++){res[i][i]=1;}// identity matrix;
      while(p>0){
            if(p%2==1){
                  mat_mul(res,a,res1,m); // multiply res*a. res1 storing res*a and again assigning it to res.
            }
            p/=2;
                 mat_mul(a,a,tmp,m);
      }
      //res contains result
 }
// end
static long[] f;

  static long phi(long n){
  	int k=(int)Math.sqrt(n);
  	long x=1,y=n;
  	for(int i=2;i<=k;i++){
  		boolean b=false;
  		while(n%i==0){
  			n/=i;b=true;
  		}
  		if(b){
  			 y/=i;
  		x*=(long)(i-1);
  		}
  	}
  	if(n>1){x*=(n-1);y/=n;}
  	return x*y;
  }
// taking inputs
static BufferedReader s;
static String read() throws IOException{String line="";while(line.length()==0){line=s.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
// ends
public static void main(String[] args) throws  IOException{
       s=new BufferedReader(new InputStreamReader(System.in));
       BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
       	StringBuilder sb=new StringBuilder();
        int t=1;
         while(t--!=0){
        	String ss=read();
        	int[] a=new int[26];
        	for(int i=0;i<ss.length();i++){a[ss.charAt(i)-'a']++;}
        		Arrays.sort(a);
        	long res=a[25]*a[24];
        	out.write(Math.max(1,res)+"\n");
       }
       out.flush();
   }
}


import java.util.*;
import java.io.*;
public class Task{
/// functions
static long gcd(long a,long b){if(b==0){return a;}return gcd(b,a%b);}
static long Modpow(long a,long p,long m){long res=1;while(p>0){if((p&1)!=0){res=(res*a)%m;}p >>=1;a=(a*a)%m;}return res;}
static long Modmul(long a,long b,long m){return ((a%m)*(b%m))%m;}
static long ModInv(long a,long m){return Modpow(a,m-2,m);}
static long nck(int n,int r,long m){if(r>n){return 0l;}return Modmul(f[n],ModInv(Modmul(f[n-r],f[r],m),m),m);}
static void mat_mul(long[][] a, long[][] b, long[][]tmp,long m){
	int n=a.length;
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
              long sum=0;
              for(int k=0;k<n;k++){
                    sum+=b[i][k]*a[k][j];
                    sum%=m;
               }
               tmp[i][j]=sum;
        }
     }
     for(int i=0;i<n;i++){a[i]=tmp[i].clone();}
}
static void fast_mat_exp(long[][] a,long m, long p){
      int n=a.length;
      long[][] res=new long[n][n];
      long[][] res1=new long[n][n];
      long[][] tmp=new long[n][n];
      for(int i=0;i<n;i++){res[i][i]=1;}// identity matrix;
      while(p>0){
            if(p%2==1){
                  mat_mul(res,a,res1,m); // multiply res*a. res1 storing res*a and again assigning it to res.
            }
            p/=2;
                 mat_mul(a,a,tmp,m);
      }
      //res contains result
 }
// end
static long[] f;

  static long phi(long n){
  	int k=(int)Math.sqrt(n);
  	long x=1,y=n;
  	for(int i=2;i<=k;i++){
  		boolean b=false;
  		while(n%i==0){
  			n/=i;b=true;
  		}
  		if(b){
  			 y/=i;
  		x*=(long)(i-1);
  		}
  	}
  	if(n>1){x*=(n-1);y/=n;}
  	return x*y;
  }
// taking inputs
static BufferedReader s;
static String read() throws IOException{String line="";while(line.length()==0){line=s.readLine();continue;}return line;}
static int int_v (String s1){return Integer.parseInt(s1);}
static long long_v(String s1){return Long.parseLong(s1);}
static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;}
static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;}
// ends
public static void main(String[] args) throws  IOException{
       s=new BufferedReader(new InputStreamReader(System.in));
       BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
       	StringBuilder sb=new StringBuilder();
        int t=1;
         while(t--!=0){
        	String ss=read();
        	long[][] a=new long[26][26];
        	Map<Character,Integer>map=new HashMap<>(); long k=0;
        	for(int i=0;i<ss.length();i++){
        		char c1=ss.charAt(i);
        		for(char c:map.keySet()){
        			a[c-'a'][c1-'a']+=map.get(c);
        		}
        		map.put(c1,map.getOrDefault(c1,0)+1);
        		k=Math.max(k,map.get(c1));
        	}
        	long res=0;
        	for(int i=0;i<26;i++){
        		for(int j=0;j<26;j++){
        			res=Math.max(a[i][j],res);
        		}
        	}
        	out.write(Math.max(k,res)+"\n");
       }
       out.flush();
   }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Line{
	public static void main(String Args[])throws IOException
	{
		BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = s.length();
		int gap = 1;
		int max = 0;
		HashMap<String, Integer> map = new HashMap<>();
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			map.put(c, map.getOrDefault(c, 0)+1);
			max = Math.max(map.get(c), max);
		}

		for( ; gap<n;gap++)
		{
			for(int i = 0;i<n;i++)
			{
				String c = s.charAt(i)+"";
				for(int j = i+gap;j<n;j+=gap)
				{
					c += s.charAt(j);
//					if(c.equals("ab"))
//						System.out.println(i+" "+j +" "+ "ab");
					map.put(c, map.getOrDefault(c, 0)+1);
					max = Math.max(map.get(c), max);
				}
			}
		}
		System.out.println(max);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Line{
	public static void main(String Args[])throws IOException
	{
		BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = s.length();
		int gap = 1;
		int max = 0;
		HashMap<String, Integer> map = new HashMap<>();
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			map.put(c, map.getOrDefault(c, 0)+1);
			max = Math.max(map.get(c), max);
		}

		for( ; gap<n;gap++)
		{
			for(int i = 0;i<n;i++)
			{
				String c = s.charAt(i)+"";
				int j = i+gap;
				if(j<n)
				{
					c += s.charAt(j);
//					if(c.equals("ab"))
//						System.out.println(i+" "+j +" "+ "ab");
					map.put(c, map.getOrDefault(c, 0)+1);
					max = Math.max(map.get(c), max);
				}
			}
		}
		System.out.println(max);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Line{
	public static void main(String Args[])throws IOException
	{
		BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = s.length();
		int gap = 1;
		long max = 0;
		HashMap<String, Long> map = new HashMap<>();
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			map.put(c, map.getOrDefault(c, 0l)+1l);
			max = Math.max(map.get(c), max);
		}
		long mat[][] = new long[26][26];
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			char c1 = s.charAt(i);
			map.put(c, map.getOrDefault(c, 1l)-1l);
			for(Map.Entry<String, Long> e: map.entrySet())
			{
				char c2 = e.getKey().charAt(0);
				long val = e.getValue();
				mat[c1-97][c2-97]+= val;
				max = Math.max(mat[c1-97][c2-97], max);
			}
		}

		System.out.println(max);
	}
}


package learning;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Line{
	public static void main(String Args[])throws IOException
	{
		BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = s.length();
		int gap = 1;
		int max = 0;
		HashMap<String, Integer> map = new HashMap<>();
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			map.put(c, map.getOrDefault(c, 0)+1);
			max = Math.max(map.get(c), max);
		}

		for( ; gap<n;gap++)
		{
			for(int i = 0;i<n;i++)
			{
				String c = s.charAt(i)+"";
				int j = i+gap;
				if(j<n)
				{
					c += s.charAt(j);
					map.put(c, map.getOrDefault(c, 0)+1);
					max = Math.max(map.get(c), max);
				}
				else
					break;
			}
		}
		System.out.println(max);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Line{
	public static void main(String Args[])throws IOException
	{
		BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = s.length();
		int gap = 1;
		int max = 0;
		HashMap<String, Integer> map = new HashMap<>();
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			map.put(c, map.getOrDefault(c, 0)+1);
			max = Math.max(map.get(c), max);
		}
		int mat[][] = new int[26][26];
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			char c1 = s.charAt(i);
			map.put(c, map.getOrDefault(c, 1)-1);
			for(Map.Entry<String, Integer> e: map.entrySet())
			{
				char c2 = e.getKey().charAt(0);
				int val = e.getValue();
				mat[c1-97][c2-97]+= val;
				max = Math.max(mat[c1-97][c2-97], max);
			}
		}

		System.out.println(max);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main{
	public static void main(String Args[])throws IOException
	{
		BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = s.length();
		int gap = 1;
		int charc[] = new int[26];
//		PriorityQueue<Pair> que  =  new PriorityQueue<>(26);
		for(int i = 0;i<n;i++)
		{
			charc[s.charAt(i)-97]++;

		}
		int max = 0;char maxi = '0';
		int secmax = 0;char secmaxi='0';
		for(int i = 0;i<26;i++)
		{
			if(max<=charc[i])
			{
				secmax = max;
				secmaxi = maxi;
				max = charc[i];
				maxi = (char)(i+97);
			}
			else
			{
				if(secmax<=charc[i])
				{
					secmax = charc[i];
					secmaxi = (char)(i+97);
				}
			}
		}

//		System.out.println(max+" "+maxi+" "+secmax+" "+secmaxi+" "+gap);
		int q = max;
		int ans1 = 0; int ans2 = 0;
		for(int i = 0;i<n;i++)
		{
			char c = s.charAt(i);
			if(c==maxi)
			{
				ans1 += secmax;
				max--;
			}
			else if(c==secmaxi)
			{
				ans2 += max;
				secmax--;
			}
		}

		System.out.println(Math.max(Math.max(ans1, ans2),q));
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Line{
	public static void main(String Args[])throws IOException
	{
		BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = s.length();
		int gap = 1;
		int max = 0;
		HashMap<String, Integer> map = new HashMap<>();
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			map.put(c, map.getOrDefault(c, 0)+1);
			max = Math.max(map.get(c), max);
		}

		for( ; gap<n;gap++)
		{
			for(int i = 0;i<n;i++)
			{
				String c = s.charAt(i)+"";
				int j = i+gap;
				if(j<n)
				{
					c += s.charAt(j);
					map.put(c, map.getOrDefault(c, 0)+1);
					max = Math.max(map.get(c), max);
				}
				else
					break;
			}
		}
		System.out.println(max);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Line{
	public static void main(String Args[])throws IOException
	{
		BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = s.length();
		int gap = 1;
		long max = 0;
		HashMap<String, Long> map = new HashMap<>();
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			map.put(c, map.getOrDefault(c, 0l)+1l);
			max = Math.max(map.get(c), max);
		}
		int mat[][] = new int[26][26];
		for(int i = 0;i<n;i++)
		{
			String c = s.charAt(i)+"";
			char c1 = s.charAt(i);
			map.put(c, map.getOrDefault(c, 1l)-1l);
			for(Map.Entry<String, Long> e: map.entrySet())
			{
				char c2 = e.getKey().charAt(0);
				long val = e.getValue();
				mat[c1-97][c2-97]+= val;
				max = Math.max(mat[c1-97][c2-97], max);
			}
		}

		System.out.println(max);
	}
}


import java.io.*;
import java.util.*;
public class cf_1307C_1500 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int l = s.length();
		int[] arr = new int[26];
		int[][] dp = new int[26][26];
		int max = 0;
		for(int i = 0; i < l; i++) {
				//System.out.println(max + " " + i + " " + j);
			for(int j = 0; j < 26; j++) {
				//System.out.println(i + " " + j + " " + (s.charAt(i)-'a') + " " + j);
				max = Math.max(max, (dp[j][s.charAt(i)-'a'] += arr[j]));
			}
			max = Math.max(max, ++arr[s.charAt(i) - 'a']);
		}
		//System.out.println(Arrays.toString(arr));
		//for(int i = 0; i < 26; i++)	System.out.println(Arrays.toString(dp[i]));
		System.out.println(max);
	}
}


import java.io.*;
import java.util.*;
public class cf_1307C_1500 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int l = s.length();
		long[] arr = new long[26];
		long[][] dp = new long[26][26];
		long max = 0l;
		for(int i = 0; i < l; i++) {
				//System.out.println(max + " " + i + " " + j);
			for(int j = 0; j < 26; j++) {
				//System.out.println(i + " " + j + " " + (s.charAt(i)-'a') + " " + j);
				max = Math.max(max, (long)(dp[j][s.charAt(i)-'a'] += arr[j]));
			}
			max = Math.max(max, ++arr[s.charAt(i) - 'a']);
		}
		//System.out.println(Arrays.toString(arr));
		//for(int i = 0; i < 26; i++)	System.out.println(Arrays.toString(dp[i]));
		System.out.println(max);
	}
}


import java.io.*;
import java.util.*;
public class cf_1307C_1500 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int l = s.length();
		int[][] arr = new int[l][26];
		int[][] dp = new int[l][676];
		int max = 0;
		for(int i = 0; i < l; i++) {
			for(int j = i; j >= 0; j--) {
				max = Math.max(max, ++arr[j][s.charAt(i) - 'a']);
				//System.out.println(max + " " + i + " " + j);
			}

		}
		for(int i = 0; i < l-1; i++) {
			for(int j = 0; j < 26; j++) {
				for(int k = i; k >= 0; k--) {
					max = Math.max(max, dp[k][(s.charAt(i)-'a')*26 + j] += arr[i+1][j]);
				}
			}
		}
		for(int i = 0; i < l; i++) {
			//System.out.println(Arrays.toString(arr[i]));
			//System.out.println(Arrays.toString(dp[i]));
		}
		System.out.println(max);
	}
}


import java.io.*;
import java.util.*;

public class TestClass {

	static PrintWriter out = new PrintWriter(System.out);
	public static void main(String[] args) throws IOException{

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String s = in.readLine();
		HashMap<Character,Integer> h = new HashMap<Character,Integer>();
		for(int i=0;i<s.length();i++) {
			char t = s.charAt(i);
			if(h.containsKey(t)) {
				h.put(t, h.get(t)+1);
			}
			else {
				h.put(t,1);
			}
		}
		if(h.size()==1) {
			out.println(h.get(s.charAt(0)));
		}
		else {
			int max1 = Integer.MIN_VALUE;
			int max2 = Integer.MIN_VALUE;
			Iterator it = h.entrySet().iterator();
			while(it.hasNext()) {
				Map.Entry<Character,Integer> map = (Map.Entry)it.next();
				int v = map.getValue();
				if(v>max1) {
					max1 = v;
				}
				else if(v>max2){
					max2 = v;
				}
			}
			out.println(max1*max2);
		}
        out.close();
	}

}


import java.io.*;
import java.util.*;

public class TestClass {

	static PrintWriter out = new PrintWriter(System.out);
	public static void main(String[] args) throws IOException{

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String s = in.readLine();
		long a1[] = new long[26];
		long a2[][] = new long[26][26];
		long ans = 0;
		for(int i=0;i<s.length();i++) {
			int a = s.charAt(i)-'a';
			for(int j=0;j<26;j++) {
				a2[j][a] += a1[j];
			}
			a1[a]++;
		}
		for(int i=0;i<26;i++) {
			ans = Math.max(ans, a1[i]);
		}
		for(int i=0;i<26;i++) {
			for(int j=0;j<26;j++) {
				ans = Math.max(ans, a2[i][j]);
			}
		}
		out.println(ans);
        out.close();
	}

}


import java.io.*;
import java.util.*;

public class TestClass {

	static PrintWriter out = new PrintWriter(System.out);
	public static void main(String[] args) throws IOException{

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String s = in.readLine();
		int a1[] = new int[26];
		int a2[][] = new int[26][26];
		int ans = 0;
		for(int i=0;i<s.length();i++) {
			int a = s.charAt(i)-'a';
			for(int j=0;j<26;j++) {
				a2[j][a] += a1[j];
			}
			a1[a]++;
		}
		for(int i=0;i<26;i++) {
			ans = Math.max(ans, a1[i]);
		}
		for(int i=0;i<26;i++) {
			for(int j=0;j<26;j++) {
				ans = Math.max(ans, a2[i][j]);
			}
		}
		out.println(ans);
        out.close();
	}

}


import java.util.*;
import java.util.logging.Level;
import java.io.*;

public class tr0 {
	static PrintWriter out;
	static StringBuilder sb;
	static final double EPS = 1e-9;
	static long mod = 1000000007;
	static int inf = (int) 1e9 + 2;
	static long[] fac;
	static int[] si;
	static ArrayList<Integer> primes;
	static ArrayList<qu>[] ad;
	static ArrayList<qu>[] d;
	static edge[] ed;
	static int[] l, ch;
	static int[] occ;
	static long[][] adjMatrix;
	static Queue<Integer>[] can;
	static String s;
	static int[][] memo, P;
	static int n, k;
	static int[] ans, p, aa, bb, qq;
	static boolean[] t;
	static HashMap<Integer, Integer> pri;
	static TreeMap<qu, Integer> map;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
		String s = sc.nextLine();
		long[] a = new long[26];
		for (int i = 0; i < s.length(); i++)
			a[s.charAt(i) - 'a']++;
		long ans = 0;
		for (int i = 0; i < 26; i++)
			ans = Math.max(ans, a[i]);
		HashMap<String, Long> h = new HashMap<>();
		for (int i = 0; i < s.length(); i++) {
			a[s.charAt(i) - 'a']--;
			for (int j = 0; j < 26; j++) {
				String w=""+s.charAt(i)+(char)(j+'a');
				if(a[j]==0)
					continue;
                   h.put(w,h.getOrDefault(w, 0l)+a[s.charAt(i)-'a']*a[j]);
			}
		//	System.out.println(Arrays.toString(a));
			//System.out.println(h);
		}
		for(String k:h.keySet())
			ans=Math.max(ans, h.get(k));
		out.print(ans);
		out.flush();
	}

	static class qu implements Comparable<qu> {
		int a;
		int b;
		int w;

		qu(int aa, int bb, int cc) {
			a = aa;
			b = bb;
			w = cc;
		}

		public String toString() {
			return a + " " + b + " " + w;
		}

		public int compareTo(qu o) {
			if (a == o.a)
				return b - o.b;
			return a - o.a;
		}
	}

	static class seg implements Comparable<seg> {
		int a;
		int b;
		int l;
		int r;
		int bit;

		seg(int s, int e, int x, int y, int bi) {
			a = s;
			b = e;
			l = x;
			r = y;
			bit = bi;
		}

		public String toString() {
			return a + " " + b + " " + l + " " + r + " " + bit;
		}

		public int compareTo(seg o) {
			// if(a==o.a)
			return bit - o.bit;
			// return
		}
	}

	static class pair implements Comparable<pair> {
		long to;
		long number;

		pair(long t, long n) {
			number = n;
			to = t;
		}

		public String toString() {
			return to + " " + number;
		}

		@Override
		public int compareTo(pair o) {
			if (o.to < to)
				return 1;
			return -1;
		}

	}

	static long modPow(long a, long e)

	{
		long res = 1;
		while (e > 0) {
			if ((e & 1) == 1)
				res = (res * a) % mod;
			a = (a * a) % mod;
			e >>= 1;
		}
		return res;
	}

	static long inver(long x) {
		long a = x;
		long e = (mod - 2);
		long res = 1;
		while (e > 0) {
			if ((e & 1) == 1) {
				res = ((1l * res * a) % mod);
			}
			a = ((1l * a * a) % mod);
			e >>= 1;
		}
		return res % mod;
	}

	static class edge implements Comparable<edge> {
		int from;
		int to;
		int number;

		edge(int f, int t, int n) {
			from = f;
			to = t;
			number = n;
		}

		public String toString() {
			return from + " " + to + " " + number;
		}

		public int compareTo(edge f) {
			return f.number - number;
		}
	}

	static void seive(int N) {
		si = new int[N];
		primes = new ArrayList<>();
		si[1] = 1;
		for (int i = 2; i < N; i++) {
			if (si[i] == 0) {
				si[i] = i;
				primes.add(i);
			}
			for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++)
				si[primes.get(j) * i] = primes.get(j);
		}
	}

	static long fac(int n) {
		if (n == 0)
			return fac[n] = 1;
		if (n == 1)
			return fac[n] = 1;
		long ans = 1;
		for (int i = 1; i <= n; i++)
			fac[i] = ans = (i % mod * ans % mod) % mod;
		return ans % mod;
	}

	static long gcd(long a, long b) {

		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	static class unionfind {
		int[] p;
		int[] size;
		int[] max;
		int num;

		unionfind(int n) {
			p = new int[n];
			size = new int[n];
			max = new int[n];
			for (int i = 0; i < n; i++) {
				p[i] = i;
				max[i] = i;
			}
			Arrays.fill(size, 1);
			num = n;
		}

		int findSet(int v) {
			if (v == p[v])
				return v;

			max[v] = Math.max(max[v], max[p[v]]);

			p[v] = findSet(p[v]);

			max[v] = Math.max(max[v], max[p[v]]);

			return p[v];
		}

		boolean sameSet(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return true;
			return false;
		}

		int max() {
			int max = 0;
			for (int i = 0; i < size.length; i++)
				if (size[i] > max)
					max = size[i];
			return max;
		}

		boolean combine(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return true;
			// System.out.println(num+" ppp");
			num--;
			if (size[a] > size[b]) {
				p[b] = a;
				max[a] = Math.max(max[a], max[b]);
				size[a] += size[b];

			} else {
				p[a] = b;
				max[b] = Math.max(max[a], max[b]);
				size[b] += size[a];
			}
			return false;
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}