#include using namespace std; int main() { int a, b, c, d, m; cin >> a >> b >> c >> d >> m; int res = 0; for (int x = a; x < b + 1; x++) { for (int y = c; y < d + 1; y++) { int tmp = (x + y) % m; if (tmp > res) res = tmp; } } cout << res << endl; return 0; }