#include #include #include #include #include #include #include #include #include #include using namespace std; int main(){ int a, b, c, d, m, min1, max1; cin >> a >> b >> c >> d >> m; min1 = a+c; max1 = b+d; if (((max1-min1) >= m-1) || (max1%m < min1%m)) cout << m-1 << endl; else cout << max1%m << endl; }