#include<bits/stdc++.h>
using namespace std;

#define endl "\n"


long long a, b, k, ost;
vector<long long> badNums;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);


    cin>>a>>b>>k;

    ost = a % k - 1;
    if(ost == -1) ost = k - 1;

    for(int i = (b / k) * k + ost + k; i - 2 >= 0; i-=k){
        badNums.push_back(i - 2);

    }
    long long curr = a, ans = 0;
    while(curr < b) {
        while(badNums.size() > 0 && (badNums.back()%2 != curr%2 || badNums.back() < curr)){
            badNums.pop_back();
        }
        if(badNums.size() == 0){
            ans += (b - curr) / 2;
            if((b - curr) % 2 != 0) ans++;
            break;
        }
        ans += (badNums.back() - curr) / 2;
        curr = badNums.back();
        badNums.pop_back();
        if(curr >= b){
            break;
        }
        ans  ++;
        curr ++;
    }
    cout<<ans<<endl;
}

