Grasshopper
The grasshopper has been sitting at position 0 on the number line for a long time, during which it developed a unique way of moving. In each cycle of movement, it makes exactly two jumps: first by a units and then by b units along the number line. If the number is positive, it jumps to the right; if negative, it jumps to the left. The time taken for each jump in seconds is equal to the number of units jumped.
For instance, if a = 3 and b = -2, the grasshopper will be at position 3 after 3 seconds. After a total of 5 seconds, it will be at position 1. Continuing this pattern, it will reach position 4 at 8 seconds and return to position 2 at 10 seconds.
Given the values a, b, and x, determine the number of seconds required for the grasshopper to reach position x on the number line. If it is impossible to reach x, output -1.
Input
Three integers a, b, and x — provided in a single line separated by spaces. The absolute values of these integers do not exceed 10^9
.
Output
The number of seconds required to reach position x, or -1 if it is not possible.