a, b, c, d, k = gets.split.map(&:to_i) puts (a * b + c - d) % k