#include using namespace std; int main(){ long long a, b, c, d, k; cin >> a >> b >> c >> d >> k; cout << (long long)(a * b + c - d) % k << endl; }