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