import Control.Applicative ((<$>)) import Data.IntSet (IntSet) import qualified Data.IntSet as IS main :: IO () main = solve <$> map read <$> words <$> getLine >>= print solve :: [Int] -> Int solve [n, d] = f IS.empty 0 0 where f st pc os | IS.member os st = pc - 1 | otherwise = f (IS.insert os st) (pc + k + 1) ((os + d * (k + 1)) `mod` n) where k = (n - 1 - os) `div` n