#include const int maxn = 1005; int a[maxn], dp[maxn]; int main() { int a, b, c, d, m; scanf("%d %d %d %d %d", &a, &b, &c, &d, &m); int res = -1; for (int i = a; i <= b; i ++) { for (int j = c; j <= d; j ++) { res = std::max(res, (i + j) % m); } } std::cout << res << "\n"; return 0; }