This problem is the same as “War Games 2” but with bigger limitations.
During the latest war games the Minister of Defense of the Soviet Federation comrade Ivanov had a good chance to make sure personally, that an alertness of the Soviet Army under his command is just brilliant. But there was a thing, that continued to worry him. Being an outstanding commander, he realized, that only physical conditions of the soldiers were demonstrated. So the time came to organize one more war games and examine their mental capacity.
General Rascal was appointed to be responsible for the war games again. The general donated the allocated funds to the poor and went to bed free-hearted. In his dream, the tactics manual appeared to him and described a scheme, that allows to organize the war games absolutely free of charge.
In accordance with this scheme, the war games are divided into N phases; and N soldiers, successively numbered from 1 to N, are marching round a circle one after another, i.e. the first follows the second, the second follows the third, ..., the (N−1)-th follows the N-th, and the N-th follows the first. At each phase, a single soldier leaves the circle and goes to clean the WC, while the others continue to march. At some phase, the circle is left by a soldier, who is marching K positions before the one, who left the circle at the previous phase. A soldier, whose number is K, leaves the circle at the first phase.
Surely, Mr. Rascal cherished no hope about his soldiers’ abilities to determine an order of leaving the circle. "These fools can not even paint the grass properly", he sniffed scornfully and went to sergeant Filcher for an assistance.
The only line contains the integer numbers N (1 ≤ N ≤ 1.1 ⋅ 106) and K (1 ≤ K ≤ N).
Since the answer (the numbers of soldiers as they leave the circle) is sometimes too large, you should output only one integer: xor of the numbers |i − si| for each i from 1 to N, where si is the number of the soldier, which has lived the circle on the i-th step.
In the example the numbers of soldiers as they leave the circle are: 3 1 5 2 4.
So, the answer is: |1 − 3| xor |1 − 2| xor |5 − 3| xor |2 − 4| xor |4 − 5| = 2 xor 1 xor 2 xor 2 xor 1 = 2.
Problem Author: Prepared by Maxim Pivko