#include using namespace std; #define rep(i, a, b) for (int i=a;i<(b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair pii; typedef vector vi; typedef vector vvi; int main() { cin.tie(0)->sync_with_stdio(0); int N; cin >> N; int res = numeric_limits::max(); while(N--) { int A,B,C,D,E; cin >> A >> B >> C >> D >> E; int currA = min(A, B); int currD = min(D, E); int f = currA + currD; A -= currA; B -= currA; D -= currD; E -= currD; if((A == 0 && D == 0) || (B == 0 && E == 0)) { res = min(res, f); continue; } f += min({max(A, D), C, max(B, E)}); res = min(res, f); } cout << res << endl; }