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