Under construction

Still being built. Things may change

Faulty Mars Rover

Interview

Time limit2sMemory limit512 MB

Summary
Starting from a, reach exactly b using steps of +1 and +2 while never landing on a multiple of c; find the fewest steps.
Level

Medium4 of 10

Topics
Math, Greedy, Implementation, Number theory
Solved
No attempts yet

Problem

A Mars rover carrying out an international mission on Mars has broken down.

To restore its operation, the rover's battery power must be increased. The battery power of the rover is given as a positive integer. The current battery power is aa, and to restore the rover's operation it must be increased to bb. To change the battery power, two types of special signals, X and Y, can be sent to the rover from Earth. A signal of type X increases the current battery power by 1, and a signal of type Y increases the current battery power by 2.

The mission organizers want to change the battery power to the required value by sending the minimum number of signals. Unfortunately, due to the rover's design, if the battery power becomes a multiple of the integer cc, the rover breaks down permanently and stops responding to signals.

Write a program that, given the initial battery power aa, the required battery power bb, and the integer cc, determines the minimum number of signals that must be sent to the rover to restore its operation.

Input

The input contains three integers aa, bb, and cc, one per line. (1≤a<b≤1091 \le a < b \le 10^9, 2≤c≤1092 \le c \le 10^9, aa is not a multiple of cc, and bb is not a multiple of cc.)

Output

Print a single integer: the minimum number of signals that must be sent to the rover.

Hint

In the first example, one can proceed as follows: send the signals Y, X, Y to the rover. The battery power changes as 2→4→5→72 \to 4 \to 5 \to 7. In the second example, one can proceed as follows: send the signals X, Y, X, Y to the rover. The battery power changes as 4→5→7→8→104 \to 5 \to 7 \to 8 \to 10.

Examples2

  1. Example 1

    Input
    2
    7
    3
    
    Expected output
    3
    
  2. Example 2

    Input
    4
    10
    3
    
    Expected output
    4