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