#include #define rep(i, l, r) for (int i = (l); i < (r); i++) using namespace std; typedef long long ll; int main() { int a, b, c, d, m, ans = 0; cin >> a >> b >> c >> d >> m; rep(x, a, b + 1) { rep(y, c, d + 1) { ans = max(ans, (x + y) % m); } } cout << ans << endl; }