0125. Joseph Problem

Имя входного файла: joseph.1n
Имя выходного файла: joseph.out
Ограничение по времени: 500 ms
Ограничение по памяти: 64 megabytes

n boys are standing in circle. They start counting themselves clockwise, starting from 1. As soon as the count reaches p, the last boy counted leaves the circle, and they continue counting from the next boy, starting from 1 again.

Last remaining boy wins.

Can you calculate his number in clockwise order, if the boy from whom the counting originally started has number 1?

Input file

Input file contains two integer numbers, n and p. (1 ≤ n,p ≤ 1000000).

Output file

Output file should contain one number – the original number of the last boy.

Examples:

joseph.1njoseph.out
3 42


Источник: Petrozavodsk Summer 2003. Blitz Kontest, Monday, August 25
Автор: Andrew Lopatin, Nick Durov

Обсудить       Отправить решение



Версия для печати