a, b = io.read("*n", "*n") c, d = io.read("*n", "*n") k = io.read("*n") x = 1LL * a * b + c - d x = x % (1LL * k) x = tostring(x):gsub("LL", "") print(x)