#include using namespace std; #define int long long using ll = long long; using ld = long double; #define print(x) cerr << #x << " = " << x << endl template ostream& operator<<(ostream &out, vector &cont) { out << "["; for (const auto &x : cont) out << x << ", "; out << "]"; return out; } int32_t main() { int N; cin >> N; int ans = INT_MAX; for (int i = 0; i < N; i++) { int a, b, c, d, e; cin >> a >> b >> c >> d >> e; int apassed = a, dpassed = d; if (b - a > 0) { int x = min(c, b - a); apassed += x; dpassed -= x; } if (e - d >= 0) { int x = min(c, e - d); dpassed += x; apassed -= x; } int cans = min(apassed, b) + min(dpassed, e); ans = min(ans, cans); } cout << ans << endl; }