#include <bits/stdc++.h>
#define TASK "asdkajsdkjasd"
#define endl '\n'
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define BIT(i,x) (((i)>>(x))&1)
#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 all(C) C.begin(), C.end()
using namespace std;
using ll = long long;
const int INT_LIM = 2147483647;
const ll LL_LIM = 9223372036854775807;
template <typename X> bool minimize(X &x, const X &y) {if (x>y){x = y; return true;}return false;}
template <typename X> bool maximize(X &x, const X &y) {if (x<y){x = y; return true;}return false;}
///------------------------------------------///
struct SegmentTree{
ll T[300000*4+5];
void build(int id, int l, int r)
{
if (l==r)
{
T[id] = LL_LIM; return;
}
int mid = (l+r)>>1;
build(id*2, l, mid);
build(id*2+1, mid+1, r);
T[id] = LL_LIM;
}
void update(int id, int l, int r, int u, ll val)
{
if (l>u || r<u) return;
if (l==r)
{
T[id] = val; return;
}
int mid = (l+r)>>1;
update(id*2, l, mid, u, val);
update(id*2+1, mid+1, r, u, val);
T[id] = min(T[id*2], T[id*2+1]);
}
ll get(int id, int l, int r, int u, int v)
{
if (l>v || r<u) return LL_LIM;
if (l>=u && r<=v) return T[id];
int mid = (l+r)>>1;
return min(get(id*2, l, mid, u, v),
get(id*2+1, mid+1, r, u, v));
}
} tree1, tree2;
int n,m,M;
struct item{
int a,b,c;
} a[100005];
ll c[100005];
void inp()
{
cin >> n >> m;
map<int, vector<int*>> CC;
CC[m].pb(&m);
FOR(i, 1, n)
{
cin >> a[i].a >> a[i].b >> a[i].c >> c[i];
CC[a[i].a].pb(&a[i].a);
CC[a[i].b].pb(&a[i].b);
CC[a[i].c].pb(&a[i].c);
}
for (auto V:CC)
{
M++;
for (auto i:V.se)
{
*i = M;
}
}
tree1.build(1, 1, M);
tree2.build(1, 1, M);
}
void solve()
{
ll ans = LL_LIM;
FOR(i, 1, n)
{
ll lterm = LL_LIM, rterm = LL_LIM;
if (a[i].a==1) lterm = 0;
else lterm = tree1.get(1, 1, M, a[i].a, a[i].b);
if (a[i].b==M) rterm = 0;
else rterm = tree2.get(1, 1, M, a[i].a, a[i].b);
if (lterm!=LL_LIM)
{
tree1.update(1, 1, M, a[i].c, lterm+c[i]);
}
if (rterm!=LL_LIM)
{
tree2.update(1, 1, M, a[i].c, rterm+c[i]);
}
// cout << i << ' ' << lterm << ' ' << rterm << endl;
if (lterm!=LL_LIM && rterm!=LL_LIM)
{
minimize(ans, lterm+rterm+c[i]);
}
}
if (ans==LL_LIM) cout << -1;
else cout << ans;
}
signed main()
{
///--------------------------///
// ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
if (fopen(TASK".INP","r")!=NULL)
{
freopen(TASK".INP","r",stdin);
freopen(TASK".OUT","w",stdout);
}
///--------------------------///
int NTEST = 1;
bool codeforces = 0;
if (codeforces) cin >> NTEST;
while (NTEST--)
{
inp();
solve();
}
return 0;
}
///------------------------------------------///
Compilation message (stderr)
pinball.cpp: In function 'int main()':
pinball.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(TASK".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | 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... |