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