#include #include #include #include using namespace std; int main(){ int N, min_sheep = INT32_MAX; cin >> N; for (int i = 0; i < N; i++){ int a,b,c,d,e; cin >> a >> b >> c >> d >> e; //cout << a << " " << b << " " << c << " " << d << " " << e << endl; //int sheep = max(min(d+c, e) + min(a-c, b), min(a+c, b) + min(d-c, e)); //int sheep = min(b, a + min(d,c)) + min(e, d + min(a,c)); //min_sheep = min(sheep, min_sheep); int sheep = 0; if (a >= b){ sheep += b; a = a - b; b = 0; } else { sheep += a; b = b - a; a = 0; } if (d >= e){ sheep += e; d = d - e; e = 0; } else { sheep += d; e = e - d; d = 0; } if (b == 0 && e == 0){ continue; } else if (b == 0){ if (a > 0 && c > 0 && e > 0){ sheep += min({a, c, e}); } } else if (e == 0){ if (d > 0 && c > 0 && b > 0){ sheep += min({d, c, b}); } } //cout << a << " " << b << " " << c << " " << d << " " << e << endl; //cout << sheep << endl; min_sheep = min(sheep, min_sheep); } cout << min_sheep << endl; return 0; }