#include using namespace std; #define ll long long int main() { int pct; cin >> pct; int mini = -1; for (int i = 0; i < pct; i++) { int a,b,c,d,e; cin >> a >> b >> c >> d >> e; int minimum = min(a,b) + min(d,e); if (a > b & d < e) { minimum += min(min(a-b,c),e-d); } if (a < b & d > e) { minimum += min(min(d-e,c),b-a); } if (mini != -1) { mini = min(mini,minimum); } else { mini = minimum; } } cout << mini; cout << endl; }