Joseph's Problem#5358

Joseph's Problem

1 second
64 megabytes
Hard (≈1650)

Joseph likes taking part in programming contests. His favorite problem is, of course, Joseph’s problem. It is stated as follows.

There are tokens, numbered from to n - 1, placed in a circle. Starting from token number , every -th token (counting from the previous one) is removed from the circle. This process repeats with the tokens that remain, always counting tokens from the previously removed one, until only one token remains in the circle. This token is called the last remaining token. The problem is: given and , determine the number assigned to the last remaining token in the original circle.

Of course, all of you know the way to solve this problem. The solution is very short, all you need is one cycle:

r := 0;

for i from 1 to n do

r := (r + k) mod i;

return r;

Here "x mod y" is the remainder of the division of by .

But Joseph is not very smart. He learned the algorithm, but did not learn the reasoning behind it. Thus, he has forgotten the details of the algorithm and remembers the solution just approximately.

He told his friend Andrew about the problem, but claimed that the solution can be found using the following algorithm:

r := 0;

for i from 1 to n do

r := r + (k mod i);

return r;

Of course, Andrew pointed out that Joseph was wrong. But calculating the function Joseph described is also very interesting.

Given and , find the requested sum.

Input

The input file contains and , where 1 \leq n, k \leq 10^9.

Output

Output the sum requested.

Examples

Example 1

Loading
Loading