#include using namespace std; size_t maxElement(size_t a, size_t b, size_t c, size_t d, size_t e) { size_t result = 0; if (b > a) { result += a; b -= a; a = 0; } else { result += b; a -= b; b = 0; } if (e > d) { result += d; e -= d; d = 0; } else { result += e; d -= e; e = 0; } if (b == 0 && e == 0) return result; if (c != 0) { if (a == 0) { a += min(d, c); d -= min(d, c); c = 0; result += maxElement(a, b, c, d, e); return result; } if (d == 0) { d += min(a, c); a -= min(a, c); c = 0; result += maxElement(a, b, c, d, e); return result; } } return result; } int main() { int n; cin >> n; size_t result = UINT64_MAX; for (int i = 0; i < n; ++i) { size_t a, b, c, d, e; cin >> a >> b >> c >> d >> e; size_t current = maxElement(a, b, c, d, e); if (result > current) result = current; } cout << result; return 0; }