Bored out of your mind on a long car ride, you decide to try out a peculiar single-player game. Upon opening the box, you discover a cardboard circle, divided into slices, as well as a single token. The topmost section is inscribed with the integer , and the segment in the clockwise direction is marked with the integer . The token is then placed on the piece labelled . You are given turns, where during each turn, you may move the token either one or two tiles forward in the clockwise direction, returning to the piece marked with after each loop.
After all the turns have elapsed, your total score is calculated by calculating the sum of values that the token ended on after each turn, meaning that the score of the tile that the token starts on during the first turn is not counted. Can you determine the maximum possible score that you can obtain after playing optimally for turns, taken modulo ?
Constraints
For all subtasks, .
Subtask 1 [30%]
Subtask 2 [70%]
Input Specification
The first and only line of input will contain the integers and , in that order.
Output Specification
Output, on a single line, the maximum score that can be achieved within moves.
Sample Input
5 5
Sample Output
18
Explanation of Sample Output
The best score you can obtain is by moving to the tiles , obtaining a final score of .
Comments