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