#include<bits/stdc++.h>
using namespace std;
long long a, b, k;
long long ans;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>a>>b>>k;
    if(b-a+1<k){
        cout<<(b-a)/2+(b-a)%2;
        return 0;
    }
    long long curr=k-a%k-1;
    b-=a;
    b+=a%k;
    ans=curr/2+curr%2+1;
    if(b-k-1>=k){
        if(k==2) curr=(b-k-1)/k;
        else curr=((b-k-1)/k)*((k-1)/2+(k-1)%2+1);
        ans+=curr;
        //cout<<curr<<endl;
    }
    curr=b%k-1;
    ans+=curr/2 + curr%2;
    cout<<ans;
    return 0;
}

