#include int minSegment() { int A,B,C,D,E; std::cin >> A >> B >> C >> D >> E; int point_A = A , point_B = D, point_C = 0; if (point_A > B) { point_A -= B; point_C += B; B = 0; } else { point_C += point_A; B -= point_A; point_A = 0; } if (point_B > E) { point_B -= E; point_C += E; E = 0; } else { point_C += point_B; E -= point_B; point_B = 0; } if ((point_A > 0 && point_B > 0) || (point_A == 0 && point_B == 0)) return point_C; if (point_A > 0) { point_B += std::min(C, point_A); point_C += std::min(E, point_B); } else { point_A += std::min(C, point_B); point_C += std::min(B, point_A); } return point_C; } int main() { int segments; std::cin >> segments; int min = minSegment(); // std::cout << min << std::endl; for (size_t i = 0; i < segments - 1; i++) { int res = minSegment(); // std::cout << res << std::endl; min = std::min(res, min); } std::cout << min << std::endl; return 0; }