#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull countTraps(ull a, ull b, ull k){
    ull toFirst = k-(a%k);
    if(toFirst > (b-a)){
        return 0;
    }
    ull fromLast = (b%k);
    ull first = (a + toFirst)/k;
    ull last = (b - fromLast)/k;
    return last-first + 1;
}
int main(){
    ull a, b, k;
    cin >> a >> b >> k;

    ull traps = countTraps(a, b, k);
    ull diff = b-a;
    ull diffEven = (diff%2)==0;
    if(traps == 0){
        cout << diff/2 + (!diffEven) << '\n';
        return 0;
    }
//    cout << "traps: " << traps << '\n';

    ull cnt;
    if(k%2 == 0){
        /// ako udrqm, to e samo vednuj
        ull hits = (a%2==0);
        if((diffEven && (hits%2==0)) || ((!diffEven) && (hits%2==1))){hits = 0;}
        ull normal = diff/2+(!diffEven);
        normal += hits/2 + hits%2;
        cout << normal << '\n';
    }else{
        ull stepsTillFirstTrap = k-(a%k);
        ull hits;
        if(stepsTillFirstTrap % 2 == 1){
            hits = traps-1;
        }else{
            hits = traps;
        }
//        cout << "hits: " << hits << '\n';

        if(hits == 0){
            cout << diff/2+(!diffEven) << '\n';
            return 0;
        }

        if((diffEven && (hits%2==0)) || ((!diffEven) && (hits%2==1))){hits--;}
        ull normal = diff/2+(!diffEven);
        normal += hits/2 + hits%2;
        cout << normal << '\n';
    }

    return 0;
}
