#include using namespace std; int64_t segment(int64_t res, int64_t a, int64_t b, int64_t c, int64_t d, int64_t e) { int64_t v1 = min(a, res); res -= v1; int64_t v2 = min(d, res); int64_t res_res = min(v1, b); v1 -= res_res; b -= res_res; int64_t res_tmp = min(v2, e); v2 -= res_tmp; e -= res_tmp; res_res += res_tmp; if(v1 && !v2) { v2 = min(c, v1); res_res += min(v2, e); } else if(!v1 && v2) { v1 = min(c, v2); res_res += min(v1, b); } return res_res; } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); int64_t n; cin >> n; int64_t a, b, c, d, e; cin >> a >> b >> c >> d >> e; int64_t res = a + d; res = segment(res, a, b, c, d, e); for(int64_t i = 1; i < n; i++) { cin >> a >> b >> c >> d >> e; res = segment(res, a, b, c, d, e); } cout << res << "\n"; return 0; }