a, b, c, d, k = map(int, input().split()) print((a * b + c - d) % k)