main = getContents >>= print . yaruki . map read . words yaruki [w,d] = fst $ until ((==1).snd) work (w,d) where work (w,d) = (w - truncate (fromIntegral w / (fromIntegral d)^2), d-1)