#abc268e. [abc268_e]Chinese Restaurant (Three-Star Version)
[abc268_e]Chinese Restaurant (Three-Star Version)
Problem Statement
Person , Person , , and Person are sitting around a turntable in counterclockwise order, evenly spaced. Dish is in front of Person on the table.
You may perform the following operation or more times:
- Rotate the turntable by one -th of a counterclockwise turn. The dish that was in front of Person right before the rotation is now in front of Person .
When you are finished, Person gains frustration of , where is the minimum integer such that Dish is in front of either Person or Person .
Find the minimum possible sum of frustration of the people.
What is ? For an integer and a positive integer , denotes the integer between and (inclusive) such that is a multiple of . (It can be proved that such is unique.)
Constraints
- if .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
4
1 2 0 3
Sample Output 1
2
The figure below shows the table after one operation.
Here, the sum of their frustration is because:
- Person gains a frustration of since Dish is in front of Person ;
- Person gains a frustration of since Dish is in front of Person ;
- Person gains a frustration of since Dish is in front of Person ;
- Person gains a frustration of since Dish is in front of Person .
We cannot make the sum of their frustration less than , so the answer is .
Sample Input 2
3
0 1 2
Sample Output 2
0
Sample Input 3
10
3 9 6 1 7 2 8 0 5 4
Sample Output 3
20