#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define bit(s, i) (((s) >> (i)) & 1)
#define ii pair <int, int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
template <class X, class Y>
    bool maximize(X &x, Y y) {
        if(x < y) {
            x = y;
            return true;
        }
        return false;
    }
template <class X, class Y>
    bool minimize(X &x, Y y) {
        if(x > y) {
            x = y;
            return true;
        }
        return false;
    }
void solve();
int32_t main() {
    #define task "test"
    if(fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
	cin.tie(0)->sync_with_stdio(0);
    solve();
}
const ll INFLL=1e18;
const int N=300005;
int m,n,a[N],b[N],c[N],d[N]; ll res=INFLL;
vector<int> rar;
struct segmentTree{
    ll st[N<<2];
    void build(int id, int l, int r){
        if(l==r){
            st[id]=INFLL;
            return;
        }  
        int mid=(l+r)>>1;
        build(id<<1,l,mid);
        build(id<<1|1,mid+1,r);
        st[id]=min(st[id<<1],st[id<<1|1]);
    }
    void upd(int x, ll val){
        int id=1, l=0, r=sz(rar)-1;
        while(l<r){
            int mid=(l+r)>>1;
            if(x<=mid){
                r=mid;
                id=id<<1;
            }
            else{
                l=mid+1;
                id=id<<1|1;
            }
        }
        st[id]=min(st[id],val);
        while(id>1){
            id/=2;
            st[id]=min(st[id<<1],st[id<<1|1]);
        }
    }  
    ll get(int u, int v, int id, int l, int r){
        if(u>r||v<l) return INFLL;
        if(u<=l&&r<=v) return st[id];
        int mid=(l+r)>>1;
        return min(get(u,v,id<<1,l,mid),get(u,v,id<<1|1,mid+1,r));
    } 
    ///
    void build(){
        build(1,0,sz(rar)-1);
    }
    ll get(int u, int v){
        return get(u,v,1,0,sz(rar)-1);
    }
} smt1,smtn;
void solve() {
    cin>>m>>n;
    rar.eb(1); rar.eb(n);
    FOR(i,1,m){
        cin>>a[i]>>b[i]>>c[i]>>d[i];
        rar.eb(a[i]); rar.eb(b[i]); rar.eb(c[i]);
    }   
    sort(all(rar));
    rar.erase(unique(all(rar)),rar.end());
 
    smt1.build(); smt1.upd(0,0);
    smtn.build(); smtn.upd(sz(rar)-1,0);
    FOR(i,1,m){
        a[i]=lower_bound(all(rar),a[i])-rar.begin();
        b[i]=lower_bound(all(rar),b[i])-rar.begin();
        c[i]=lower_bound(all(rar),c[i])-rar.begin();
        minimize(res,smt1.get(a[i],b[i])+smtn.get(a[i],b[i])+d[i]);
        smt1.upd(c[i],smt1.get(a[i],b[i])+d[i]);
        smtn.upd(c[i],smtn.get(a[i],b[i])+d[i]);
    }    
    FOR(i,0,sz(rar)-1) minimize(res,smt1.get(i,i)+smtn.get(i,i));
    cout<<((res==INFLL)?-1:res)<<'\n';
}   
Compilation message (stderr)
pinball.cpp: In function 'int32_t main()':
pinball.cpp:39:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |                 freopen(task".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:40:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |                 freopen(task".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |