Hide

Problem K
Knitting Pattern

Languages en is
/problems/knittingpattern/file/statement/en/img-0001.jpg
Image by Linn Bryhn Jacobsen, from commons.wikimedia.org

Jörmunrekur had found himself with some extra time on his hands, so he decided to try to find a new hobby. After discussing this with some of his relatives, his grandparents lent him a book with knitting guides and knitting patterns.

He wants to start with something big, so he decides to make a sweater. He has also picked out a pattern from the book that he will repeat around the circumference of the sweater. He wants the pattern to be centered and then repeat out towards the back in either direction, but never wants to have less than the full pattern on the sweater. He will not place any patterns that leaves the placements of patterns asymmetric. Now he has to know how much empty space he should leave at the back of the sweater to achieve this.

The empty space that is not covered by the patterns must be a contiguous (possibly empty) section at the back of the sweater.

Input

The input contains two positive integers $N$, the length of the sweater, and $P$, the length of the pattern. They satisfy $1 \leq P \leq N \leq 10^{18}$ and they have the same parity, as otherwise the pattern could never be perfectly centered. The input is all on one line, with the integers separated by a space.

Output

Print a single integer, the amount of empty space left on the back of the sweater.


Explanation of samples

In the first sample the sweater is 13 loops in circumference. Thus the centered pattern is placed at loops 6, 7 and 8. There’s space for another pattern in either direction at loops 3, 4, 5 and 9, 10, 11. There’s not enough space to place two more, and a single pattern would make things asymmetric. Thus loops 1, 2, 12 and 13 are empty, so the answer is 4.

\includegraphics[width=0.15\textwidth ]{knittingpatterns_1_pattern}
Figure 1: A possible pattern for sample 1
\includegraphics[width=0.65\textwidth ]{knittingpatterns_1_full}
Figure 2: Applying the pattern to the full width for sample 1

In the second sample the sweater is 16 loops in circumference. The first pattern is placed at loops 7, 8, 9 and 10. Two more are placed at 3, 4, 5, 6 and 11, 12, 13, 14. This leaves 1, 2, 15 and 16, which exactly fits one more pattern that will be perfectly centered at the back of the sweater, creating no asymmetry. Thus that pattern is placed, leaving no empty space.

\includegraphics[width=0.2\textwidth ]{knittingpatterns_2_pattern}
Figure 3: A possible pattern for sample 2
\includegraphics[width=0.8\textwidth ]{knittingpatterns_2_full}
Figure 4: Applying the pattern to the full width for sample 2
Sample Input 1 Sample Output 1
13 3
4
Sample Input 2 Sample Output 2
16 4
0

Please log in to submit a solution to this problem

Log in