/** * author: ytsmash * created: 03.09.2021 21:22:44 **/ #include using namespace std; typedef long long ll; typedef long double ld; struct edge { int to, cost; }; #define rep(i,n) for(int i = 0; i < (n); i++) #define all(x) x.begin(), x.end() templatebool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } const long double EPS = 1e-10; const ll INF = 1e18; const long double PI = acos(-1.0L); using P = pair; vector devisor(int num) { vector ret; for (int i = 1; i * i <= num; i++) { if (num % i == 0) { ret.push_back(i); if (i * i != num) { ret.push_back(num / i); } } } sort(ret.begin(), ret.end()); return ret; } vector> prime_factorize(ll N) { vector> res; for (ll a = 2; a * a <= N; ++a) { if (N % a != 0) continue; ll ex = 0; while (N % a == 0) { ++ex; N /= a; } res.push_back({a, ex}); } if (N != 1) res.push_back({N, 1}); return res; } int main() { int a, b, c, d, m; cin >> a >> b >> c >> d >> m; int ans = 0; for (int i = a; i <= b; i++) { for (int j = c; j <= d; j++) { chmax(ans, (i + j) % m); } } cout << ans << endl; return 0; }