#include using namespace std; int a, b, c, d, m, t = 0; int main(){ cin >> a >> b >> c >> d >> m; for(int i = a; i <= b; i++){ for(int j = c; j <= d; j++){ t = max(t, (i + j) % m); } } cout << t << endl; }