Wowo is a hospitable Xinjiang uncle.

guests will have Uyghur Polo (a traditional Uyghur food) in Wowo's house around a big round table.

(

) chairs are placed around the table uniformly. Each guest sits on a chair and no two guests sit on the same chair.

bowls of Uyghur Polo are on the table. Each bowl is next to some chair (
with or without some guest sitting on it). No two bowls locate at the same position.
As a waiter, you are supposed to assign each person with exactly one bowl of Uyghur Polo. The table can be rotated, so each time you can turn it

degrees clockwise or counterclockwise. The bowls turn with the table while the chairs and guests do not move. When one bowl of Uyghur Polo is in front of a guest, he can either take it or wait for another.
You want to minimize the total times of table rotating so that everybody can have meals as quickly as possible.
(Formal definition: The boundary of the table is a circle.

chairs are at

points on the circle whose convex hull is a regular polygon with

vertices. We name the points

in counterclockwise order. The

-th bowl is at point

(

) initially. The

-th guest is at point

(

) initially. If you turn the table counterclockwise, the bowl at point

(

) will be moved to point
%20%5Cbmod%20n)
after the rotation. If you turn the table clockwise, the bowl at point

(

) will be moved to point
%20%5Cbmod%20n)
after the rotation. (

is defined as the smallest nonnegative integer

such that

is a multiple of

.)