#include #include #include #include using namespace std; void fix(vector>& v) { vector> nv; sort(v.begin(), v.end()); for(auto [a, b]: v) { if(nv.empty() || nv.back()[1] < a-1) { nv.push_back({a, b}); } else { nv.back()[1] = max(nv.back()[1], b); } } v.swap(nv); } int main() { int n, abase, bbase; cin >> n >> abase >> bbase; vector>>> dp(2); for(auto& x: dp) x.resize(n+1); dp[0][0].push_back({0, 0}); for(int qq = 0; qq < n; qq++) { int src = qq%2; int dst = 1^src; dp[dst] = dp[src]; int al, ar, bl, br; cin >> al >> ar >> bl >> br; al -= abase; ar -= abase; bl -= bbase; br -= bbase; if(ar < 0 || br < 0) continue; for(int i = max(al, 0); i <= min(ar, qq); i++) { for(auto [a, b]: dp[src][i]) { int nlhs = max(a, bl); int nrhs = min(b, br); if(nlhs <= nrhs) { dp[dst][i+1].push_back({nlhs, nrhs}); dp[dst][i].push_back({nlhs+1, nrhs+1}); } } } for(int i = 0; i <= qq+1; i++) fix(dp[dst][i]); } int ret = 0; int dst = (n%2); for(int i = 0; i <= n; i++) { if(dp[dst][i].size()) ret = max(ret, i + dp[dst][i].back()[1]); } cout << ret << "\n"; }