#include #include size_t min (size_t a, size_t b, size_t c) { size_t m = SIZE_MAX; if (b < m) { m = b; } if (a < m) { m = a; } if (c < m) { m = c; } return m; } int main() { size_t n; std::cin >> n; size_t res = SIZE_MAX; for (size_t i = 0; i < n; i++) { size_t a, b, c, d, e; size_t tmp; std::cin >> a >> b >> c >> d >> e; if (a >= b && d >= e) { tmp = b + e; } else if (a <= b && d <= e) { tmp = a + d; } else if (a > b && d < e) { tmp = b + d + min (c, a-b, e-d); } else if (a < b && d > e) { tmp = a + e + min (c, b-a, d-e); } if (tmp < res) { res = tmp; } } std::cout << res << std::endl; return 0; }