#include using namespace std; using ll = long long; #define dbl long double #define vec vector #define um unordered_map #define us unordered_set using ull = unsigned long long; using vi = vec; using vl = vector; using vd = vector; using pii = pair; using pll = pair; using pdd = pair; inline ll min(ll a, ll b) { return a < b ? a : b; } ll solve() { ll A, B, C, D, E; cin >> A >> B >> C >> D >> E; ll up = min(A, B); ll down = min(D, E); ll ru = A > B ? (A - B) : 0; ll rd = D > E ? (D - E) : 0; if ((ru == 0 && rd == 0) || (ru != 0 && rd != 0)) { return up + down; } else { if (ru == 0) { return up + down + min(C, min(B - A, rd)); } else { return up + down + min(C, min(E - D, ru)); } } } int main(){ cin.sync_with_stdio(false); cin.tie(0); cin.exceptions(ios::failbit); int n; cin >> n; ll min_flow = 1e18; while (n--) { ll poss = solve(); if (poss < min_flow) min_flow = poss; } cout << min_flow << endl; }