#include #include using namespace std; int main() { int A, B, C, D, M; cin >> A >> B >> C >> D >> M; int max = -1; for (int x=A; x <= B; x++) { for (int y=C; y <= D; y++) { max = (max < fmod(x+y, M)) ? fmod(x+y, M) : max; } } cout << max << endl; }