a, b, c, d, k = map(int, input().split()) print(f"{(a*b+c-d) % k}\n")