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