{-# LANGUAGE TypeApplications #-} main = do [a, b, c, d, k] <- map (read @Integer) . words <$> getLine print $ (a * b + c - d) `mod` k