#include using namespace std; using ll = long long; using ull = unsigned long long; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int res = 500000000; for(int i = 0; i < n; ++i) { int a, b, c, d, e; cin >> a >> b >> c >> d >> e; int tp = 0; int mn = min(a, b); tp += mn; a -= mn; b -= mn; mn = min(d, e); tp += mn; d -= mn; e -= mn; tp += max(min({a, c, e}), min({d, c, b})); res = min(res, tp); } cout << res << '\n'; return 0; }