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