A,B,C,D,K = map(int, input().split()) print((A*B+C-D)%K)