#include #include int min (int a, int b, int c) { if (a < b) { if (a < c) return a; else return c; } if (c < b) return c; return b; } int main() { int n = 0; std::cin >> n; int res = INT16_MAX; for (int i = 0; i < n; i++) { int a, b, c, d, e; int tmp = 0; 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 tmp = a + e + min (c, b-a, d-e); if (tmp < res) res = tmp; } std::cout << res <