#include<bits/stdc++.h> #include<atcoder/all> using namespace std; using namespace atcoder; using ll = long long; #define rep(i,n) for(int i=0; i<n; ++i) int main(){ int a, b, c, d, m; cin >> a >> b >> c >> d >> m; int ans = -1; for(int x = a; x <= b; ++x){ for(int y = c; y <= d; ++y){ int tmp = (x + y) % m; ans = max(ans, tmp); } } cout << ans << endl; return 0; }