#include size_t segment(size_t sheepTop, size_t limitTop, size_t limitMid, size_t sheepBot, size_t limitBot) { size_t through = 0; if (sheepTop > limitTop) { through += limitTop; sheepTop -= limitTop; limitTop = 0; } else { through += sheepTop; limitTop -= sheepTop; sheepTop = 0; } if (sheepBot > limitBot) { through += limitBot; sheepBot -= limitBot; limitBot = 0; } else { through += sheepBot; limitBot -= sheepBot; sheepBot = 0; } size_t botToTop = std::min(sheepBot, std::min(limitMid, limitTop)); size_t topToBot = std::min(sheepTop, std::min(limitMid, limitBot)); through += std::max(botToTop, topToBot); return through; } int main() { size_t segments; std::cin >> segments; size_t through = -1; for (size_t i = 0; i < segments; ++i) { size_t a, b, c, d, e; std::cin >> a >> b >> c >> d >> e; size_t thisSegment = segment(a, b, c, d, e); if (thisSegment < through) through = thisSegment; } std::cout << through << std::endl; return 0; }