#include using namespace std; #ifdef LOCAL #include "settings/debug.cpp" #define _GLIBCXX_DEBUG #else #define Debug(...) void(0) #endif using ll = long long; #define rep(i, n) for (int i = 0; i < (n); ++i) 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++) { ans = max(ans, (i + j) % m); } } cout << ans << endl; return 0; }