#include using namespace std; using ll = long long; #ifdef LOCAL #include "debug.hpp" #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); ll A, B; cin >> A >> B; ll C, D; cin >> C >> D; ll M; cin >> M; ll ans = 0; for (ll i = A; i <= B; i++) { for (ll j = C; j <= D; j++) { ans = max(ans, (i + j) % M); } } cout << ans << '\n'; return 0; }