#include #include int main() { int N; long long result = INT64_MAX; std::cin >> N; for (int i = 0; i < N; i++) { long long a,b,c,d,e; long long value; std::cin >> a >> b >> c >> d >> e; long long e_rest = e - std::min(e, d); long long d_rest = d - std::min(e, d); long long a_rest = a - std::min(a, b); long long b_rest = b - std::min(a, b); value = std::min(a,b) + std::min(e,d); // std::cout << "i=" << i << " " << value << std::endl; long long max_end = std::max(e_rest, b_rest); long long max_start = std::max(a_rest, d_rest); value += std::min({max_end, max_start, c}); // std::cout << "i=" << i << " " << value << std::endl; // b = std::max(0LL, b-a); // e = std::max(0LL, e-d); // if (b > a) { // long long x = std::max(0LL, d - e); // value = std::min(a, b) + std::min(c, std::min(b - a, x)); // } else { // long long x = std::max(0LL, a - b); // value = std::min(d, e) + std::min(c, std::min(e - d, x)); // } result = std::min(value, result); } std::cout << result; }