This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h> 
#include <chrono> 
#include <ext/pb_ds/assoc_container.hpp> 
   
using namespace std;
using namespace __gnu_pbds;
   
template  <typename T > 
using oset = tree < T, null_type, less < T > ,  rb_tree_tag,  tree_order_statistics_node_update > ;
   
#define int long long
       
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(v) v.begin(),  v.end()
#define rall(v) v.rbegin(),  v.rend()
       
#define endl '\n'
using ll = long long;
using ld = long double;
       
       
#define send ios::sync_with_stdio(false);
#define help cin.tie(0);
       
void solve(int T);
       
const int N = 250;
const int M = 405;
const int SQRT = sqrt(N);
const int LOG = 20;
const int INF = 1e12;
const int MOD = 45678;
const ld EPS = 1e-9;
       
       
int ans;
int n,  m,  q,  l,  r,  x,  y,  mx,  mn;
       
       
int32_t main(){
    send help;
    solve(1);
}
struct Dev{
    ll i,  a,  b,  c,  d;
    ll dp[2] = {INF,  INF};
    bool operator < (const Dev o) const {
        return b < o.b;
    }
};
Dev A[N];
void solve(int T){
    cin>>n>>m;
    for(int i = 0;i<n;i++){
        cin>>A[i].a>>A[i].b>>A[i].c>>A[i].d;
        A[i].i = i;
    }
    sort(A, A+n);
    ans = INF;
    for(int i = 0;i<n;i++){
        if(A[i].a == 1) A[i].dp[0] = A[i].dp[1] = A[i].d;
        for(int j = 0;j<n;j++){
            if(A[i].i > A[j].i && A[i].a <= A[j].c && A[j].c <= A[i].b)A[i].dp[0] = min(A[i].dp[0], A[j].dp[0] + A[i].d);
            else if(A[j].i > A[i].i && A[j].a <= A[i].c && A[i].c <= A[j].b)A[i].dp[1] = min(A[i].dp[1], min(A[j].dp[0] + A[i].d, A[j].dp[1] + A[i].d));
            
        }
        if(A[i].b  == m){
            ans = min(ans, min(A[i].dp[0], A[i].dp[1]));
        }
    }
    if(ans == INF) cout<<-1<<endl;
    else cout<<ans<<endl;
}  
//Te molam da raboti !!
| # | 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... |