#include #include #include #include #include #include #include #include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //cout << fixed << setprecision(15); int a , b , c , d , m; cin >> a >> b >> c >> d >> m; if (a + c <= m && m <= b + d) cout << m - 1 << endl; else cout << (b + d) % m << endl; return 0; }