#include using namespace std; int main() { // Input int a, b, c, d, m; cin >> a >> b >> c >> d >> m; // All Search int ans = 0; for (int x = a; x <= b; ++x) { for (int y = c; y <= d; ++y) { ans = max(ans, (x + y) % m); } } // Output cout << ans << endl; }